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

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

Introduction

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

Prototype

public Email setStartTLSEnabled(final boolean startTlsEnabled) 

Source Link

Document

Set or disable the STARTTLS encryption.

Usage

From source file:Control.CommonsMail.java

/**
 * Classe que envia E-amil//  w  w  w. j  a  va 2s  .  c  o  m
 * @throws EmailException
 */
public void enviaEmailSimples(String Msg) throws EmailException {

    Email email = new SimpleEmail();
    email.setDebug(true);
    email.setHostName("smtp.gmail.com"); // o servidor SMTP para envio do e-mail
    //email.setHostName("smtp.pharmapele.com.br"); // o servidor SMTP para envio do e-mail
    email.setSmtpPort(587);
    email.setSSLOnConnect(true);
    email.setStartTLSEnabled(true);
    email.setAuthentication("softwaredeveloperantony@gmail.com", "tony#020567");
    //email.setAuthentication("antony@pharmapele.com.br", "tony#020567");

    //email.setFrom("softwaredeveloperantony@gmail.com"); // remetente
    email.setFrom("antony@pharmapele.com.br"); // remetente
    email.setSubject("Exporta Estoque lojas"); // assunto do e-mail
    email.setMsg(Msg); //conteudo do e-mail
    email.addTo("antony@pharmapele.com.br", "Antony"); //destinatrio

    //email.sets(true);
    //email.setTLS(true);
    try {

        email.send();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.mirth.connect.connectors.smtp.DefaultSmtpConfiguration.java

@Override
public void configureEncryption(ConnectorProperties connectorProperties, Email email) throws Exception {
    SmtpDispatcherProperties props = (SmtpDispatcherProperties) connectorProperties;

    if ("SSL".equalsIgnoreCase(props.getEncryption())) {
        email.setSSLOnConnect(true);//from   w ww. ja va 2s.  c  o m
        email.setSslSmtpPort(props.getSmtpPort());
    } else if ("TLS".equalsIgnoreCase(props.getEncryption())) {
        email.setStartTLSEnabled(true);
    }
}

From source file:edu.corgi.uco.sendEmails.java

public void sendConfirmation(String email2, String firstName, String lastName, int token, int id)
        throws EmailException {

    Email email = new SimpleEmail();

    email.setDebug(true);/*from  w ww .j av  a 2s .c o  m*/
    email.setHostName("smtp.gmail.com");
    email.setAuthenticator(new DefaultAuthenticator("ucocorgi2@gmail.com", "ucodrsung"));
    email.setStartTLSEnabled(true);
    email.setSmtpPort(587);
    email.setFrom("ucocorgi@gmail.com", "UCO CS Corgi");
    email.setSubject("Account Confirmation");
    email.setMsg(firstName + " " + lastName
            + " please go to the following address http://localhost:8080/Corgi/faces/accountAuth.xhtml "
            + "and enter the token:" + token + " and the ID:" + id + " to confirm and activate your account");
    System.out.print("Email Address: " + email2);
    email.addTo(email2);

    email.send();

}

From source file:edu.corgi.uco.sendEmails.java

public String send(String emailAddress, String studentFirstName, String studentLastName) throws EmailException {

    System.out.print("hit send");
    Email email = new SimpleEmail();
    System.out.print("created email file");
    email.setDebug(true);//from w  ww  .ja  v a2  s  . c  om
    email.setHostName("smtp.gmail.com");
    email.setAuthenticator(new DefaultAuthenticator("ucocorgi2@gmail.com", "ucodrsung"));
    email.setStartTLSEnabled(true);
    email.setSmtpPort(587);
    email.setFrom("ucocorgi@gmail.com", "UCO CS Secretary");
    email.setSubject("Advisement Update");
    email.setMsg(studentFirstName + " " + studentLastName
            + " your advisment has been processed and the hold on your account will be removed shortly");
    System.out.print("Email Address: " + emailAddress);
    email.addTo(emailAddress);

    System.out.print("added values");

    email.send();
    System.out.print("sent");

    return null;
}

From source file:com.irurueta.server.commons.email.ApacheMailSender.java

/**
 * Internal method to send email using Apache Mail.
 * @param m email message./*from   ww w  .  j  a v  a 2s. c o m*/
 * @param email apache email message.
 * @throws NotSupportedException if feature is not supported.
 * @throws EmailException if Apache Mail cannot send email.
 * @throws com.irurueta.server.commons.email.EmailException if sending
 * email fails.
 */
private void internalSendApacheEmail(EmailMessage m, Email email)
        throws NotSupportedException, EmailException, com.irurueta.server.commons.email.EmailException {
    email.setHostName(mMailHost);
    email.setSmtpPort(mMailPort);
    if (mMailId != null && !mMailId.isEmpty() && mMailPassword != null && !mMailPassword.isEmpty()) {
        email.setAuthenticator(new DefaultAuthenticator(mMailId, mMailPassword));
    }
    email.setStartTLSEnabled(true);
    email.setFrom(mMailFromAddress);
    if (m.getSubject() != null) {
        email.setSubject(m.getSubject());
    }
    m.buildContent(email);

    //add destinatoins
    for (String s : (List<String>) m.getTo()) {
        email.addTo(s);
    }

    for (String s : (List<String>) m.getCC()) {
        email.addCc(s);
    }

    for (String s : (List<String>) m.getBCC()) {
        email.addBcc(s);
    }

    email.send();
}

From source file:com.mirth.connect.server.util.ServerSMTPConnection.java

public void send(String toList, String ccList, String from, String subject, String body, String charset)
        throws EmailException {
    Email email = new SimpleEmail();

    // Set the charset if it was specified. Otherwise use the system's default.
    if (StringUtils.isNotBlank(charset)) {
        email.setCharset(charset);/*w w w  .ja v a 2  s . c o m*/
    }

    email.setHostName(host);
    email.setSmtpPort(Integer.parseInt(port));
    email.setSocketConnectionTimeout(socketTimeout);
    email.setDebug(true);

    if (useAuthentication) {
        email.setAuthentication(username, password);
    }

    if (StringUtils.equalsIgnoreCase(secure, "TLS")) {
        email.setStartTLSEnabled(true);
    } else if (StringUtils.equalsIgnoreCase(secure, "SSL")) {
        email.setSSLOnConnect(true);
        email.setSslSmtpPort(port);
    }

    // These have to be set after the authenticator, so that a new mail session isn't created
    ConfigurationController configurationController = ControllerFactory.getFactory()
            .createConfigurationController();
    email.getMailSession().getProperties().setProperty("mail.smtp.ssl.protocols", StringUtils.join(
            MirthSSLUtil.getEnabledHttpsProtocols(configurationController.getHttpsClientProtocols()), ' '));
    email.getMailSession().getProperties().setProperty("mail.smtp.ssl.ciphersuites", StringUtils.join(
            MirthSSLUtil.getEnabledHttpsCipherSuites(configurationController.getHttpsCipherSuites()), ' '));

    for (String to : StringUtils.split(toList, ",")) {
        email.addTo(to);
    }

    if (StringUtils.isNotEmpty(ccList)) {
        for (String cc : StringUtils.split(ccList, ",")) {
            email.addCc(cc);
        }
    }

    email.setFrom(from);
    email.setSubject(subject);
    email.setMsg(body);
    email.send();
}

From source file:com.aurel.track.util.emailHandling.MailSender.java

private Email setSecurityMode(Email email) {
    Integer smtpSecurity = smtpMailSettings.getSecurity();
    String oldTrustStore = (String) System.clearProperty("javax.net.ssl.trustStore");
    LOGGER.debug("oldTrustStore=" + oldTrustStore);
    switch (smtpSecurity) {
    case TSiteBean.SECURITY_CONNECTIONS_MODES.NEVER:
        LOGGER.debug("SMTP security connection mode is NEVER");
        break;//from   www .j  av a2  s  .  co  m
    case TSiteBean.SECURITY_CONNECTIONS_MODES.TLS_IF_AVAILABLE:
        LOGGER.debug("SMTP security connection mode is TLS_IF_AVAILABLE");
        email.setStartTLSEnabled(true);
        MailBL.setTrustKeyStore(smtpMailSettings.getHost());
        break;
    case TSiteBean.SECURITY_CONNECTIONS_MODES.TLS:
        LOGGER.debug("SMTP security connection mode is TLS");
        email.setStartTLSEnabled(true);
        email.setStartTLSRequired(true);
        MailBL.setTrustKeyStore(smtpMailSettings.getHost());
        break;
    case TSiteBean.SECURITY_CONNECTIONS_MODES.SSL: {
        LOGGER.debug("SMTP security connection mode is SSL");
        MailBL.setTrustKeyStore(smtpMailSettings.getHost());
        email.setSSLOnConnect(true);
        break;
    }
    default:
        break;
    }
    return email;
}

From source file:fr.gael.dhus.messaging.mail.MailServer.java

public void send(Email email, String to, String cc, String bcc, String subject) throws EmailException {
    email.setHostName(getSmtpServer());//  ww  w .  ja v a2 s  .c  o m
    email.setSmtpPort(getPort());
    if (getUsername() != null) {
        email.setAuthentication(getUsername(), getPassword());
    }
    if (getFromMail() != null) {
        if (getFromName() != null)
            email.setFrom(getFromMail(), getFromName());
        else
            email.setFrom(getFromMail());
    }
    if (getReplyto() != null) {
        try {
            email.setReplyTo(ImmutableList.of(new InternetAddress(getReplyto())));
        } catch (AddressException e) {
            logger.error("Cannot configure Reply-to (" + getReplyto() + ") into the mail: " + e.getMessage());
        }
    }

    // Message configuration
    email.setSubject("[" + cfgManager.getNameConfiguration().getShortName() + "] " + subject);
    email.addTo(to);

    // Add CCed
    if (cc != null) {
        email.addCc(cc);
    }
    // Add BCCed
    if (bcc != null) {
        email.addBcc(bcc);
    }

    email.setStartTLSEnabled(isTls());
    try {
        email.send();
    } catch (EmailException e) {
        logger.error("Cannot send email: " + e.getMessage());
        throw e;
    }
}

From source file:adams.core.net.SimpleApacheSendEmail.java

/**
 * Sends an email.//from w  w w  .  j  a  v  a2s.  c o  m
 *
 * @param email   the email to send
 * @return      true if successfully sent
 * @throws Exception   in case of invalid internet addresses or messaging problem
 */
@Override
public boolean sendMail(Email email) throws Exception {
    org.apache.commons.mail.Email mail;
    String id;
    MultiPartEmail mpemail;
    EmailAttachment attachment;

    if (email.getAttachments().length > 0) {
        mail = new MultiPartEmail();
        mpemail = (MultiPartEmail) mail;
        for (File file : email.getAttachments()) {
            attachment = new EmailAttachment();
            attachment.setPath(file.getAbsolutePath());
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setName(file.getName());
            mpemail.attach(attachment);
        }
    } else {
        mail = new SimpleEmail();
    }
    mail.setFrom(email.getFrom().getValue());
    for (EmailAddress address : email.getTo())
        mail.addTo(address.getValue());
    for (EmailAddress address : email.getCC())
        mail.addCc(address.getValue());
    for (EmailAddress address : email.getBCC())
        mail.addBcc(address.getValue());
    mail.setSubject(email.getSubject());
    mail.setMsg(email.getBody());
    mail.setHostName(m_Server);
    mail.setSmtpPort(m_Port);
    mail.setStartTLSEnabled(m_UseTLS);
    mail.setSSLOnConnect(m_UseSSL);
    if (m_RequiresAuth)
        mail.setAuthentication(m_User, m_Password.getValue());
    mail.setSocketTimeout(m_Timeout);
    try {
        id = mail.send();
        if (isLoggingEnabled())
            getLogger().info("Message sent: " + id);
    } catch (Exception e) {
        getLogger().log(Level.SEVERE, "Failed to send email: " + mail, e);
        return false;
    }

    return true;
}

From source file:com.mirth.connect.server.controllers.DefaultConfigurationController.java

@Override
public ConnectionTestResponse sendTestEmail(Properties properties) throws Exception {
    String portString = properties.getProperty("port");
    String encryption = properties.getProperty("encryption");
    String host = properties.getProperty("host");
    String timeoutString = properties.getProperty("timeout");
    Boolean authentication = Boolean.parseBoolean(properties.getProperty("authentication"));
    String username = properties.getProperty("username");
    String password = properties.getProperty("password");
    String to = properties.getProperty("toAddress");
    String from = properties.getProperty("fromAddress");

    int port = -1;
    try {//ww  w . j  a v  a2 s  . com
        port = Integer.parseInt(portString);
    } catch (NumberFormatException e) {
        return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE,
                "Invalid port: \"" + portString + "\"");
    }

    Email email = new SimpleEmail();
    email.setDebug(true);
    email.setHostName(host);
    email.setSmtpPort(port);

    try {
        int timeout = Integer.parseInt(timeoutString);
        email.setSocketTimeout(timeout);
        email.setSocketConnectionTimeout(timeout);
    } catch (NumberFormatException e) {
        // Don't set if the value is invalid
    }

    if ("SSL".equalsIgnoreCase(encryption)) {
        email.setSSLOnConnect(true);
        email.setSslSmtpPort(portString);
    } else if ("TLS".equalsIgnoreCase(encryption)) {
        email.setStartTLSEnabled(true);
    }

    if (authentication) {
        email.setAuthentication(username, password);
    }

    // These have to be set after the authenticator, so that a new mail session isn't created
    ConfigurationController configurationController = ControllerFactory.getFactory()
            .createConfigurationController();
    String protocols = properties.getProperty("protocols", StringUtils.join(
            MirthSSLUtil.getEnabledHttpsProtocols(configurationController.getHttpsClientProtocols()), ' '));
    String cipherSuites = properties.getProperty("cipherSuites", StringUtils.join(
            MirthSSLUtil.getEnabledHttpsCipherSuites(configurationController.getHttpsCipherSuites()), ' '));
    email.getMailSession().getProperties().setProperty("mail.smtp.ssl.protocols", protocols);
    email.getMailSession().getProperties().setProperty("mail.smtp.ssl.ciphersuites", cipherSuites);

    SSLSocketFactory socketFactory = (SSLSocketFactory) properties.get("socketFactory");
    if (socketFactory != null) {
        email.getMailSession().getProperties().put("mail.smtp.ssl.socketFactory", socketFactory);
        if ("SSL".equalsIgnoreCase(encryption)) {
            email.getMailSession().getProperties().put("mail.smtp.socketFactory", socketFactory);
        }
    }

    email.setSubject("Mirth Connect Test Email");

    try {
        for (String toAddress : StringUtils.split(to, ",")) {
            email.addTo(toAddress);
        }

        email.setFrom(from);
        email.setMsg(
                "Receipt of this email confirms that mail originating from this Mirth Connect Server is capable of reaching its intended destination.\n\nSMTP Configuration:\n- Host: "
                        + host + "\n- Port: " + port);

        email.send();
        return new ConnectionTestResponse(ConnectionTestResponse.Type.SUCCESS,
                "Sucessfully sent test email to: " + to);
    } catch (EmailException e) {
        return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, e.getMessage());
    }
}