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

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

Introduction

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

Prototype

public Email setStartTLSRequired(final boolean startTlsRequired) 

Source Link

Document

Set or disable the required STARTTLS encryption.

Usage

From source file:com.github.triceo.robozonky.notifications.email.AbstractEmailingListener.java

private static Email createNewEmail(final EmailNotificationProperties properties) throws EmailException {
    final Email email = new SimpleEmail();
    email.setCharset(Defaults.CHARSET.displayName());
    email.setHostName(properties.getSmtpHostname());
    email.setSmtpPort(properties.getSmtpPort());
    email.setStartTLSRequired(properties.isStartTlsRequired());
    email.setSSLOnConnect(properties.isSslOnConnectRequired());
    email.setAuthentication(properties.getSmtpUsername(), properties.getSmtpPassword());
    email.setFrom(properties.getSender(), "RoboZonky @ " + properties.getLocalHostAddress());
    email.addTo(properties.getRecipient());
    return email;
}

From source file:at.treedb.util.Mail.java

public static void sendMail(String smtpHost, int smtpPort, TransportSecurity transportSecurity, String smtpUser,
        String smtpPassword, String mailTo, String mailFrom, String subject, String message) throws Exception {
    Objects.requireNonNull(mailTo, "Mail.sendMail(): mailTo can not be null!");
    Objects.requireNonNull(subject, "Mail.sendMail(): subject can not be null!");
    Objects.requireNonNull(message, "Mail.sendMail(): message can not be null!");
    Email email = new SimpleEmail();
    email.setHostName(smtpHost);/*w  w  w  . j  a v a2s .  c  o m*/

    email.setAuthenticator(new DefaultAuthenticator(smtpUser, smtpPassword));
    if (transportSecurity == TransportSecurity.SSL) {
        email.setSSLOnConnect(true);
        email.setSSLCheckServerIdentity(false);
    } else if (transportSecurity == TransportSecurity.STARTTLS) {
        email.setStartTLSRequired(true);
    }
    email.setSmtpPort(smtpPort);
    if (mailFrom != null && !mailFrom.isEmpty()) {
        email.setFrom(smtpUser);
    }
    email.setSubject(subject);
    email.setMsg(message);
    email.addTo(mailTo);
    email.send();
}

From source file:com.qwazr.connectors.EmailConnector.java

public void sendEmail(Email email) throws EmailException {
    email.setHostName(hostname);/*ww  w  .  j  a va 2 s  .  com*/
    if (ssl != null)
        email.setSSLOnConnect(ssl);
    if (start_tls_enabled != null)
        email.setStartTLSEnabled(start_tls_enabled);
    if (start_tls_required != null)
        email.setStartTLSRequired(start_tls_required);
    if (port != null)
        email.setSmtpPort(port);
    if (username != null)
        email.setAuthentication(username, password);
    if (connection_timeout != null)
        email.setSocketConnectionTimeout(connection_timeout);
    if (timeout != null)
        email.setSocketTimeout(timeout);
    email.send();
}

From source file:com.qwazr.library.email.EmailConnector.java

public void sendEmail(final Email email) throws EmailException {
    email.setHostName(hostname);/*from w ww  .  j  ava2s.c o m*/
    if (ssl != null)
        email.setSSLOnConnect(ssl);
    if (start_tls_enabled != null)
        email.setStartTLSEnabled(start_tls_enabled);
    if (start_tls_required != null)
        email.setStartTLSRequired(start_tls_required);
    if (port != null)
        email.setSmtpPort(port);
    if (username != null)
        email.setAuthentication(username, password);
    if (connection_timeout != null)
        email.setSocketConnectionTimeout(connection_timeout);
    if (timeout != null)
        email.setSocketTimeout(timeout);
    email.send();
}

From source file:json.JsonUI.java

public void enviandoEmail() {
    Properties login = new Properties();
    String properties = "json/login.properties";
    try {//from w ww  .j  a va2  s.c om
        InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream(properties);
        login.load(stream);
    } catch (IOException ex) {
        System.out.println("Erro: " + ex.getMessage());
    }
    String username = login.getProperty("hotmail.username");
    String password = login.getProperty("hotmail.password");

    try {
        Email email = new SimpleEmail();
        email.setHostName("smtp.live.com");
        email.setSmtpPort(587);
        email.setStartTLSRequired(true);
        email.setAuthenticator(new DefaultAuthenticator(username, password));
        email.setFrom(username);
        email.setSubject(assuntoEmail.getText());
        email.setMsg(jTextAreaMsgEmail.getText());
        email.addTo(emailDestino.getText());
        email.setDebug(true);
        email.send();
        aviso.setText("Mensagem enviada.");
    } catch (EmailException ex) {
        System.out.println("Erro: " + ex.getMessage());
    }

}

From source file:ch.sdi.core.impl.mail.MailSenderDefault.java

/**
 * @see ch.sdi.core.intf.MailSender#sendMail(java.lang.Object)
 */// w  ww .  j a va  2  s .  c o m
@Override
public void sendMail(Email aMail) throws SdiException {
    aMail.setHostName(myHost);
    if (mySslOnConnect) {
        aMail.setSslSmtpPort("" + myPort);
    } else {
        aMail.setSmtpPort(myPort);
    } // if..else mySslOnConnect

    aMail.setAuthenticator(myAuthenticator);
    aMail.setSSLOnConnect(mySslOnConnect);
    aMail.setStartTLSRequired(myStartTlsRequired);

    try {
        aMail.setFrom(mySenderAddress);
    } catch (EmailException t) {
        throw new SdiException("Problems setting the sender address to mail: " + mySenderAddress, t,
                SdiException.EXIT_CODE_MAIL_ERROR);
    }

    if (myDryRun) {
        myLog.debug("DryRun is set. Not sending the mail");
        // TODO: save locally in output dir
    } else {
        try {
            aMail.send();
            myLog.debug("mail successfully sent");
        } catch (Throwable t) {
            throw new SdiException("Problems sending a mail", t, SdiException.EXIT_CODE_MAIL_ERROR);
        }
    } // if..else myDryRun

}

From source file:at.treedb.util.Mail.java

/**
 * /*from  ww w.ja  v  a  2 s. com*/
 * @param sendTo
 * @param subject
 * @param message
 * @throws EmailException
 */
public void sendMail(String mailTo, String mailFrom, String subject, String message) throws Exception {
    Objects.requireNonNull(mailTo, "Mail.sendMail(): mailTo can not be null!");
    Objects.requireNonNull(mailFrom, "Mail.sendMail(): mailFrom can not be null!");
    Objects.requireNonNull(subject, "Mail.sendMail(): subject can not be null!");
    Objects.requireNonNull(message, "Mail.sendMail(): message can not be null!");
    Email email = new SimpleEmail();
    email.setHostName(smtpHost);

    email.setAuthenticator(new DefaultAuthenticator(smtpUser, smtpPassword));
    if (transportSecurity == TransportSecurity.SSL) {
        email.setSSLOnConnect(true);
        email.setSSLCheckServerIdentity(false);
    } else if (transportSecurity == TransportSecurity.STARTTLS) {
        email.setStartTLSRequired(true);
    }
    email.setSmtpPort(smtpPort);
    email.setFrom(mailFrom);
    email.setSubject(subject);
    email.setMsg(message);
    email.addTo(mailTo);
    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;//www. ja  v a2 s  .c om
    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:org.apache.nifi.processors.email.TestListenSMTP.java

@Test
public void validateSuccessfulInteractionWithTls() throws Exception, EmailException {
    System.setProperty("mail.smtp.ssl.trust", "*");
    System.setProperty("javax.net.ssl.keyStore", "src/test/resources/localhost-ks.jks");
    System.setProperty("javax.net.ssl.keyStorePassword", "localtest");
    int port = NetworkUtils.availablePort();

    TestRunner runner = TestRunners.newTestRunner(ListenSMTP.class);
    runner.setProperty(ListenSMTP.SMTP_PORT, String.valueOf(port));
    runner.setProperty(ListenSMTP.SMTP_MAXIMUM_CONNECTIONS, "3");
    runner.setProperty(ListenSMTP.SMTP_TIMEOUT, "10 seconds");

    // Setup the SSL Context
    SSLContextService sslContextService = new StandardSSLContextService();
    runner.addControllerService("ssl-context", sslContextService);
    runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE,
            "src/test/resources/localhost-ts.jks");
    runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE_PASSWORD, "localtest");
    runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE_TYPE, "JKS");
    runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE,
            "src/test/resources/localhost-ks.jks");
    runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE_PASSWORD, "localtest");
    runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE_TYPE, "JKS");
    runner.enableControllerService(sslContextService);

    // and add the SSL context to the runner
    runner.setProperty(ListenSMTP.SSL_CONTEXT_SERVICE, "ssl-context");
    runner.setProperty(ListenSMTP.CLIENT_AUTH, SSLContextService.ClientAuth.NONE.name());
    runner.assertValid();/*from  w  w w  .  j a  v a  2 s  . c om*/

    int messageCount = 5;
    CountDownLatch latch = new CountDownLatch(messageCount);
    runner.run(messageCount, false);

    this.executor.schedule(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < messageCount; i++) {
                try {
                    Email email = new SimpleEmail();
                    email.setHostName("localhost");
                    email.setSmtpPort(port);
                    email.setFrom("alice@nifi.apache.org");
                    email.setSubject("This is a test");
                    email.setMsg("MSG-" + i);
                    email.addTo("bob@nifi.apache.org");

                    // Enable STARTTLS but ignore the cert
                    email.setStartTLSEnabled(true);
                    email.setStartTLSRequired(true);
                    email.setSSLCheckServerIdentity(false);
                    email.send();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                } finally {
                    latch.countDown();
                }
            }
        }
    }, 1500, TimeUnit.MILLISECONDS);

    boolean complete = latch.await(5000, TimeUnit.MILLISECONDS);
    runner.shutdown();
    assertTrue(complete);
    runner.assertAllFlowFilesTransferred("success", messageCount);
}

From source file:org.fao.geonet.util.MailUtil.java

/**
 * Create data information to compose the mail
 *
 * @param hostName/* www  .  j  a v a 2 s  .  c o m*/
 * @param smtpPort
 * @param from
 * @param username
 * @param password
 * @param email
 * @param ssl
 * @param tls
 * @param ignoreSslCertificateErrors
 */
private static void configureBasics(String hostName, Integer smtpPort, String from, String username,
        String password, Email email, Boolean ssl, Boolean tls, Boolean ignoreSslCertificateErrors) {
    if (hostName != null) {
        email.setHostName(hostName);
    } else {
        throw new IllegalArgumentException(
                "Missing settings in System Configuration (see Administration menu) - cannot send mail");
    }
    if (StringUtils.isNotBlank(smtpPort + "")) {
        email.setSmtpPort(smtpPort);
    } else {
        throw new IllegalArgumentException(
                "Missing settings in System Configuration (see Administration menu) - cannot send mail");
    }
    if (username != null) {
        email.setAuthenticator(new DefaultAuthenticator(username, password));
    }

    email.setDebug(true);

    if (tls != null && tls) {
        email.setStartTLSEnabled(tls);
        email.setStartTLSRequired(tls);
    }

    if (ssl != null && ssl) {
        email.setSSLOnConnect(ssl);
        if (StringUtils.isNotBlank(smtpPort + "")) {
            email.setSslSmtpPort(smtpPort + "");
        }
    }

    if (ignoreSslCertificateErrors != null && ignoreSslCertificateErrors) {
        try {
            Session mailSession = email.getMailSession();
            Properties p = mailSession.getProperties();
            p.setProperty("mail.smtp.ssl.trust", "*");

        } catch (EmailException e) {
            // Ignore the exception. Can't be reached because the host name is always set above or an
            // IllegalArgumentException is thrown.
        }
    }

    if (StringUtils.isNotBlank(from)) {
        try {
            email.setFrom(from);
        } catch (EmailException e) {
            throw new IllegalArgumentException(
                    "Invalid 'from' email setting in System Configuration (see Administration menu) - cannot send "
                            + "mail",
                    e);
        }
    } else {
        throw new IllegalArgumentException(
                "Missing settings in System Configuration (see Administration menu) - cannot send mail");
    }
}