Java Code Examples for org.apache.commons.mail.HtmlEmail#setStartTLSEnabled()

The following examples show how to use org.apache.commons.mail.HtmlEmail#setStartTLSEnabled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmailMessageSender.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private HtmlEmail getHtmlEmail( String hostName, int port, String username, String password, boolean tls,
    String sender ) throws EmailException
{
    HtmlEmail email = new HtmlEmail();
    email.setHostName( hostName );
    email.setFrom( sender, getEmailName() );
    email.setSmtpPort( port );
    email.setStartTLSEnabled( tls );

    if ( username != null && password != null )
    {
        email.setAuthenticator( new DefaultAuthenticator( username, password ) );
    }

    return email;
}
 
Example 2
Source File: ReportMailer.java    From AisAbnormal with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Send email with subject and message body.
 * @param subject the email subject.
 * @param body the email body.
 */
public void send(String subject, String body) {
    try {
        HtmlEmail email = new HtmlEmail();
        email.setHostName(configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_HOST, "localhost"));
        email.setSmtpPort(configuration.getInt(CONFKEY_REPORTS_MAILER_SMTP_PORT, 465));
        email.setAuthenticator(new DefaultAuthenticator(
            configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_USER, "anonymous"),
            configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_PASS, "guest")
        ));
        email.setStartTLSEnabled(false);
        email.setSSLOnConnect(configuration.getBoolean(CONFKEY_REPORTS_MAILER_SMTP_SSL, true));
        email.setFrom(configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_FROM, ""));
        email.setSubject(subject);
        email.setHtmlMsg(body);
        String[] receivers = configuration.getStringArray(CONFKEY_REPORTS_MAILER_SMTP_TO);
        for (String receiver : receivers) {
            email.addTo(receiver);
        }
        email.send();
        LOG.info("Report sent with email to " + email.getToAddresses().toString() + " (\"" + subject + "\")");
    } catch (EmailException e) {
        LOG.error(e.getMessage(), e);
    }
}
 
Example 3
Source File: SendMailSsl.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sets properties to the given HtmlEmail object based on the port from which it will be sent.
 *
 * @param email the email object to configure
 * @param port the configured outgoing port
 */
private void setPropertiesForPort(HtmlEmail email, int port) throws EmailException {
    switch (port) {
        case 587:
            String oAuth2Token = settings.getProperty(EmailSettings.OAUTH2_TOKEN);
            if (!oAuth2Token.isEmpty()) {
                if (Security.getProvider("Google OAuth2 Provider") == null) {
                    Security.addProvider(new OAuth2Provider());
                }
                Properties mailProperties = email.getMailSession().getProperties();
                mailProperties.setProperty("mail.smtp.ssl.enable", "true");
                mailProperties.setProperty("mail.smtp.auth.mechanisms", "XOAUTH2");
                mailProperties.setProperty("mail.smtp.sasl.enable", "true");
                mailProperties.setProperty("mail.smtp.sasl.mechanisms", "XOAUTH2");
                mailProperties.setProperty("mail.smtp.auth.login.disable", "true");
                mailProperties.setProperty("mail.smtp.auth.plain.disable", "true");
                mailProperties.setProperty(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oAuth2Token);
                email.setMailSession(Session.getInstance(mailProperties));
            } else {
                email.setStartTLSEnabled(true);
                email.setStartTLSRequired(true);
            }
            break;
        case 25:
            if (settings.getProperty(EmailSettings.PORT25_USE_TLS)) {
                email.setStartTLSEnabled(true);
                email.setSSLCheckServerIdentity(true);
            }
            break;
        case 465:
            email.setSslSmtpPort(Integer.toString(port));
            email.setSSLOnConnect(true);
            break;
        default:
            email.setStartTLSEnabled(true);
            email.setSSLOnConnect(true);
            email.setSSLCheckServerIdentity(true);
    }
}