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

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

Introduction

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

Prototype

String MAIL_TRANSPORT_TLS

To view the source code for org.apache.commons.mail Email MAIL_TRANSPORT_TLS.

Click Source Link

Usage

From source file:org.oscarehr.util.EmailUtils.java

/**
 * This method will return an HtmlEmail object populated with 
 * the values passed in, ignoring the parameters in the configuration file.
 *//*from  w w  w  . j  a  va2 s  .com*/
public static HtmlEmail getHtmlEmail(String smtpServer, String smtpPort, String smtpUser, String smtpPassword,
        String connectionSecurity) throws EmailException {
    logger.debug("smtpServer=" + smtpServer + ", smtpSslPort=" + smtpPort + ", smtpUser=" + smtpUser
            + ", smtpPassword=" + smtpPassword + ",connectionSecurity=" + connectionSecurity);

    HtmlEmail email = null;

    if (RECIPIENT_OVERRIDE_KEY != null || printInsteadOfSend)
        email = new HtmlEmailWrapper();
    else
        email = new HtmlEmail();

    email.setHostName(smtpServer);

    if (smtpUser != null && smtpPassword != null)
        email.setAuthentication(smtpUser, smtpPassword);

    Session session = email.getMailSession();

    if (connectionSecurity != null) {
        if (connectionSecurity.equals(CONNECTION_SECURITY_STARTTLS)) {
            session.getProperties().setProperty(Email.MAIL_TRANSPORT_TLS, "true");
            email.setTLS(true);
        } else if (connectionSecurity.equals(CONNECTION_SECURITY_SSL)) {
            email.setSSL(true);
        }
    }

    if (smtpPort != null) {
        email.setSslSmtpPort(smtpPort);
    }

    Properties properties = session.getProperties();
    properties.setProperty("mail.smtp.connectiontimeout", "20000");
    properties.setProperty("mail.smtp.timeout", "20000");

    return (email);
}