Example usage for org.apache.commons.mail MultiPartEmail setStartTLSEnabled

List of usage examples for org.apache.commons.mail MultiPartEmail setStartTLSEnabled

Introduction

In this page you can find the example usage for org.apache.commons.mail MultiPartEmail setStartTLSEnabled.

Prototype

public Email setStartTLSEnabled(final boolean startTlsEnabled) 

Source Link

Document

Set or disable the STARTTLS encryption.

Usage

From source file:com.github.somi92.seecsk.util.email.EmailSender.java

public static void sendEmail(EmailContainer ec) throws RuntimeException {

    try {/*from   w  w  w  .ja v a2  s.com*/

        String user = Config.vratiInstancu().vratiVrednost(Constants.OrgInfoConfigKeys.ORGANISATION_EMAIL);
        String password = Config.vratiInstancu()
                .vratiVrednost(Constants.OrgInfoConfigKeys.ORGANISATION_EMAIL_PASSWORD);

        EmailAttachment ea = new EmailAttachment();
        ea.setPath(ec.getAttachmentPath());
        ea.setDisposition(EmailAttachment.ATTACHMENT);
        ea.setDescription("Primer ispravno popunjene uplatnice za ?lanarinu");
        ea.setName("uplatnica.pdf");

        MultiPartEmail mpe = new MultiPartEmail();
        mpe.setDebug(true);
        mpe.setAuthenticator(new DefaultAuthenticator(user, password));
        mpe.setHostName(
                Config.vratiInstancu().vratiVrednost(Constants.EmailServerConfigKeys.EMAIL_SERVER_HOST));
        mpe.setSSLOnConnect(true);
        mpe.setStartTLSEnabled(true);
        mpe.setSslSmtpPort(
                Config.vratiInstancu().vratiVrednost(Constants.EmailServerConfigKeys.EMAIL_SERVER_PORT));
        mpe.setSubject(ec.getSubject());
        mpe.setFrom(ec.getFromEmail(),
                Config.vratiInstancu().vratiVrednost(Constants.OrgInfoConfigKeys.ORGANISATION_NAME));
        mpe.setMsg(ec.getMessage());

        mpe.addTo(ec.getToEmail());
        mpe.attach(ea);

        mpe.send();

    } catch (EmailException ex) {
        ex.printStackTrace();
        throw new RuntimeException("Sistem nije uspeo da poalje email. Pokuajte ponovo.");
    }
}

From source file:br.com.cobranca.util.Util.java

/**
 * Metodo que envia email/*from  w  w w  . j  av  a 2 s.c  om*/
 *
 * @param divida
 * @param boleto
 */
public static void enviarEmail(Divida divida, String boleto) {
    MultiPartEmail emailTemp = new MultiPartEmail();

    try {

        emailTemp.setDebug(true);
        emailTemp.setHostName("smtp.gmail.com");
        emailTemp.setSmtpPort(587);
        emailTemp.setStartTLSEnabled(true);
        emailTemp.setAuthentication("homework.fca@gmail.com", "homework@");
        emailTemp.setFrom("syscob@sycob.com.br", "SYSCOB");
        emailTemp.addTo(divida.getDevedor().getEmail());
        emailTemp.setSubject("Boleto - Syscob ");
        emailTemp.setMsg("Ol, " + divida.getDevedor().getNome() + "\n"
                + "Segue anexo Boleto referente a negociao de cdigo: " + divida.getId() + "\n\n"
                + "Obs: Favor confirmar o recebimento deste. \n\n\n\n" + "Att \n" + "Syscob");

        boleto = boleto.replace("/", File.separator);

        File f = new File(boleto);
        EmailAttachment attachment = new EmailAttachment();
        attachment.setPath(f.getPath());
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        attachment.setName(f.getName());
        emailTemp.attach(attachment);

        emailTemp.send();

    } catch (Exception e) {
        System.out.println("Erro: " + e.getMessage());
    } finally {

    }

}

From source file:com.dominion.salud.pedicom.negocio.tools.MAILTest.java

@Ignore
@Test/*from  w  ww .  j a  v a2 s.  c  om*/
public void send() {
    try {
        MultiPartEmail email = new MultiPartEmail();
        email.setHostName(SMTP);
        email.setAuthenticator(new DefaultAuthenticator(USUARIO, PASSWORD));
        email.setDebug(true);
        email.setSmtpPort(PUERTO);
        email.setStartTLSEnabled(true);

        email.setSSLOnConnect(true);

        email.setFrom(FROM, FROM);
        email.addTo(TO, TO);
        email.setSubject("Correo de Prueba");
        email.attach(new File("/home/jcgonzalez/TMP/a.hl7"));

        email.send();
    } catch (Exception e) {
        logger.error(e.toString());
    }
}

From source file:info.toegepaste.controller.StudentController.java

public void stuurMail(Student student) {
    //create attachment
    EmailAttachment attachment = new EmailAttachment();
    attachment.setDescription("PDF met uw punten");
    attachment.setName(student.getFirstname() + " " + student.getLastname() + " scores");

    MultiPartEmail email = new MultiPartEmail();
    email.setStartTLSEnabled(true);
    email.setHostName("smtp.googlemail.com");
    email.setSmtpPort(465);//from w  ww .  j av  a  2 s  .  c  o  m
    email.setAuthentication("bitmescoretracker@gmail.com", "bitmeScore");
    try {
        email.setFrom("bitmescoretracker@gmail.com", "bitme Scoretracker");
        email.setSubject("Gevraagde scores , scoretracker");
        email.setMsg("In de bijlage vind u een pdf met uw scores");
        email.attach(attachment);
        email.send();
    } catch (EmailException ex) {
    }
}

From source file:eu.ggnet.dwoss.mandator.api.value.Mandator.java

/**
 * Prepares a eMail to be send direct over the mandator smtp configuration.
 * The email is missing: to, subject, message and optional attachments.
 *
 * @return the email/*  w ww. ja v  a 2s.  com*/
 * @throws EmailException if something is wrong in the subsystem.
 */
public MultiPartEmail prepareDirectMail() throws EmailException {
    MultiPartEmail email = new MultiPartEmail();
    email.setHostName(smtpConfiguration.getHostname());
    email.addBcc(company.getEmail());
    email.setFrom(company.getEmail(), company.getEmailName());
    email.setAuthentication(smtpConfiguration.getSmtpAuthenticationUser(),
            smtpConfiguration.getSmtpAuthenticationPass());
    email.setStartTLSEnabled(false);
    email.setSSLCheckServerIdentity(false);
    email.setSSLOnConnect(false);
    email.setCharset(smtpConfiguration.getCharset());
    return email;
}

From source file:com.dominion.salud.pedicom.negocio.tools.MAILService.java

/**
 * configura el ciente de correo y envia el correo con un adjunto
 *
 * @param to//  w  w w. j  a  va2 s .c o m
 * @param attach el archivo que se adjunta en el correo
 * @param centro nombre del centro desde el que se envia
 * @throws Exception
 */
public void sendByMail(String to, Object attach, String centro) throws Exception {
    logger.debug("          Iniciando la configuracion del mail con sus parametros correspondientes ,"
            + " obtenemos los parametros de environment");

    String dataBase = routingDataSource.dbActual();
    allDataSources.getDatasources();

    Datasources dat = null;
    for (Datasources datas : allDataSources.getDatasources()) {
        if (datas.getNombreDatasource().equals(dataBase)) {
            dat = datas;
            break;
        }
    }
    if (StringUtils.isBlank(StringUtils.trim(dat.getHost()))) {
        throw new Exception(
                "El host es [" + StringUtils.trim(dat.getHost()) + "] , error de host desconocido o erroneo");
    }
    logger.debug("          El host es [" + StringUtils.trim(dat.getHost()) + "]");

    if (StringUtils.isBlank(StringUtils.trim(dat.getUsernameEmail()))) {
        throw new Exception("El usuario es [" + StringUtils.trim(dat.getUsernameEmail())
                + "], error de login en el correo , usuario desconocido o erroneo");
    }
    logger.debug("          El usuario es [" + StringUtils.trim(dat.getUsernameEmail()) + "]");

    if (StringUtils.isBlank(StringUtils.trim(dat.getPasswordEmail()))) {
        throw new Exception("La contrasea es [" + StringUtils.trim(dat.getPasswordEmail())
                + "], error de login en el correo , contrasea desconocida o erronea");
    }
    logger.debug("          La contrasea es [" + StringUtils.trim(dat.getPasswordEmail()) + "]");

    if (StringUtils.isBlank(to)) {
        throw new Exception("El destinatario es [" + to + "], no se especificaron destinatarios.");
    }
    logger.debug("          El destinatario es [" + to + "]");

    /*if (dat.getPort()== null) {
    throw new Exception("El puerto es [" + StringUtils.trim(environment.getProperty("mail.port")) + "], el puerto del servidor de correo falla");
    }*/
    logger.debug("          El puerto es [" + dat.getPort() + "]");

    /*if (StringUtils.isBlank(StringUtils.trim(environment.getProperty("mail.TLS")))) {
    throw new Exception("El TLS es [" + StringUtils.trim(environment.getProperty("mail.TLS")) + "]");
    }*/
    logger.debug("          El TLS es [" + dat.getTLS() + "]");

    System.setProperty("mail.imap.auth.plain.disable", "true");

    MultiPartEmail email = new MultiPartEmail();
    email.setHostName(StringUtils.trim(dat.getHost()));
    email.setAuthenticator(new DefaultAuthenticator(StringUtils.trim(dat.getUsernameEmail()),
            StringUtils.trim(dat.getPasswordEmail())));
    email.setFrom(StringUtils.trim(StringUtils.trim(dat.getUsernameEmail())), centro);
    email.setDebug(true);
    email.setSmtpPort(dat.getPort());
    email.setStartTLSEnabled(dat.getTLS());
    email.setSSLOnConnect(dat.getSSL());
    DataSource source = null;
    if (attach != null) {
        logger.debug("          Adjuntando pdf");
        source = new ByteArrayDataSource((InputStream) attach, "application/pdf");
        email.attach(source, "Pedido de centro " + centro + ".pdf", "Pedido de centro " + centro);
    }
    email.setSubject("Pedido de centro [" + centro + "]");
    logger.debug("          Realizando envio");
    email.addTo(to);
    email.send();

    try {
        if (!StringUtils.isBlank(StringUtils.trim(dat.getMailCC()))) {
            logger.debug("          Enviando a CC: " + dat.getMailCC());
            MultiPartEmail emailCC = new MultiPartEmail();
            emailCC.setHostName(StringUtils.trim(dat.getHost()));
            emailCC.setAuthenticator(new DefaultAuthenticator(StringUtils.trim(dat.getUsernameEmail()),
                    StringUtils.trim(dat.getPasswordEmail())));
            emailCC.setFrom(StringUtils.trim(StringUtils.trim(dat.getUsernameEmail())), centro);
            emailCC.setDebug(true);
            emailCC.setSmtpPort(dat.getPort());
            emailCC.setStartTLSEnabled(dat.getTLS());
            emailCC.setSSLOnConnect(dat.getSSL());
            if (attach != null) {
                logger.debug("          Adjuntando pdf a copia");
                emailCC.attach(source, "Pedido de centro " + centro + ".pdf", "Pedido de centro " + centro);
            }
            emailCC.setSubject("Pedido de centro " + centro);
            emailCC.addCc(StringUtils.split(StringUtils.trim(dat.getMailCC()), ","));
            emailCC.send();
        }
    } catch (Exception e) {
        logger.warn("          Se han producido errores al enviar los CC: " + e.toString());
    }
    logger.debug("     Finalizando envio email");

}

From source file:com.commander4j.email.JeMail.java

public void postMail(String recipientsTO[], String subject, String message, String attachmentFilename,
        String attachmentLongFilename) throws MessagingException {

    logger.debug("SMTP_AUTH_REQD=" + SMTP_AUTH_REQD);
    logger.debug("MAIL_SMTP_HOST_NAME=" + SMTP_HOST_NAME);
    logger.debug("MAIL_SMTP_AUTH_USER=" + SMTP_AUTH_USER);
    logger.debug("MAIL_SMTP_AUTH_PWD=********");
    logger.debug("MAIL_SMTP_PORT=" + MAIL_SMTP_PORT);
    logger.debug("MAIL_SMTP_SSL_PORT=" + MAIL_SMTP_SSL_PORT);
    logger.debug("MAIL_SMTP_FROM_ADRESS=" + SMTP_FROM_ADRESS);
    logger.debug("MAIL_SMTP_USE_SSL=" + SMTP_USE_SSL);

    //Email email = new SimpleEmail();
    logger.debug("Creating MultiPart Email");
    MultiPartEmail email = new MultiPartEmail();

    logger.debug("Setting Host Name to " + SMTP_HOST_NAME);
    email.setHostName(SMTP_HOST_NAME);/*from w w w . jav a2 s  . c  o m*/
    logger.debug("Setting SMTP Port to " + MAIL_SMTP_PORT);
    email.setSmtpPort(Integer.valueOf(MAIL_SMTP_PORT));

    logger.debug("Setting SMTP SSL Port to " + MAIL_SMTP_SSL_PORT);
    email.setSslSmtpPort(MAIL_SMTP_SSL_PORT);

    logger.debug("Setting Use SSL on Connect to  " + SMTP_USE_SSL);
    email.setSSLOnConnect(Boolean.valueOf(SMTP_USE_SSL));

    logger.debug("Authentication Required =  " + SMTP_AUTH_REQD);

    if (SMTP_AUTH_REQD.toUpperCase().equals("TRUE")) {
        email.setAuthenticator(new DefaultAuthenticator(SMTP_AUTH_USER, SMTP_AUTH_PWD));

    }

    logger.debug("Setting SMTP USE SSL =  " + SMTP_USE_SSL);
    email.setSSLOnConnect(Boolean.valueOf(SMTP_USE_SSL));

    logger.debug("Setting SMTP USE TLS =  " + SMTP_USE_TLS);
    email.setStartTLSEnabled(Boolean.valueOf(SMTP_USE_TLS));
    email.setStartTLSRequired(Boolean.valueOf(SMTP_USE_TLS));

    try {
        logger.debug("From Address =  " + SMTP_FROM_ADRESS);
        email.setFrom(SMTP_FROM_ADRESS);
        email.setSubject(subject);
        email.setMsg(message + "\n\n");
        for (int x = 1; x <= recipientsTO.length; x++) {
            logger.debug("Add To Address =  " + recipientsTO[x - 1]);
            email.addTo(recipientsTO[x - 1]);
        }

        if (JUtility.replaceNullStringwithBlank(attachmentFilename).equals("") == false) {
            // Create the attachment
            EmailAttachment attachment = new EmailAttachment();
            attachment.setPath(attachmentLongFilename);
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription(attachmentFilename);
            attachment.setName(attachmentFilename);

            // add the attachment
            logger.debug("Add Attachment");
            email.attach(attachment);
        }

        logger.debug("Sending");
        email.send();
        logger.debug("Sent successfully");
    } catch (EmailException e) {
        logger.error("Unable to send email : " + e.getCause().getMessage());
    }

}

From source file:org.betaomega.finalmaveninvitationfx.GmailProvider.java

public void send(String address, String subject, String body, String invitationName, String invitationPath,
        String invitationMimeType) throws InvitationNotFoundException, UnsupportedEncodingException

{
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath(invitationPath);/*from   w  ww. jav  a2s .c  om*/
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setName(invitationName);
    MultiPartEmail email = new MultiPartEmail();
    email.setHostName("smtp.gmail.com");
    email.setSmtpPort(587);
    email.setAuthenticator(new DefaultAuthenticator(this.fromAddress, this.password));
    email.setStartTLSEnabled(true);

    email.setTLS(true);
    try {
        email.addTo(address);
    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        email.setFrom(this.fromAddress, this.fromName);
    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }
    email.setSubject(subject);
    try {
        email.setMsg(body);
    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        email.attach(attachment);
    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        email.send();
    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }

}