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

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

Introduction

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

Prototype

public void setSslSmtpPort(final String sslSmtpPort) 

Source Link

Document

Sets the SSL port to use for the SMTP transport.

Usage

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

/**
 * @see ch.sdi.core.intf.MailSender#sendMail(java.lang.Object)
 *//*from w  w  w.j  ava 2s  . c om*/
@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: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 ww  w  . jav a2 s  .  c o m*/
        email.setSslSmtpPort(props.getSmtpPort());
    } else if ("TLS".equalsIgnoreCase(props.getEncryption())) {
        email.setStartTLSEnabled(true);
    }
}

From source file:com.pax.pay.trans.receipt.paperless.AReceiptEmail.java

private int setBaseInfo(EmailInfo emailInfo, Email email) {
    try {//from w  ww.  j ava  2 s  .c o  m
        //email.setDebug(true);
        email.setHostName(emailInfo.getHostName());
        email.setSmtpPort(emailInfo.getPort());
        email.setAuthentication(emailInfo.getUserName(), emailInfo.getPassword());
        email.setCharset("UTF-8");
        email.setSSLOnConnect(emailInfo.isSsl());
        if (emailInfo.isSsl())
            email.setSslSmtpPort(String.valueOf(emailInfo.getSslPort()));
        email.setFrom(emailInfo.getFrom());
    } catch (EmailException e) {
        e.printStackTrace();
        return -1;
    }
    return 0;
}

From source file:com.googlecode.fascinator.messaging.EmailNotificationConsumer.java

private void sendEmails(List<String> toList, List<String> ccList, String subject, String body,
        String fromAddress, String fromName, boolean isHtml) throws EmailException {
    Email email = null;
    if (isHtml) {
        email = new HtmlEmail();
        ((HtmlEmail) email).setHtmlMsg(body);
    } else {// w ww.  j  a  v a2 s. com
        email = new SimpleEmail();
        email.setMsg(body);
    }
    email.setDebug(debug);
    email.setHostName(smtpHost);
    if (smtpUsername != null || smtpPassword != null) {
        email.setAuthentication(smtpUsername, smtpPassword);
    }
    email.setSmtpPort(smtpPort);
    email.setSslSmtpPort(smtpSslPort);
    email.setSSL(smtpSsl);
    email.setTLS(smtpTls);
    email.setSubject(subject);

    for (String to : toList) {
        email.addTo(to);
    }
    if (ccList != null) {
        for (String cc : ccList) {
            email.addCc(cc);
        }
    }
    email.setFrom(fromAddress, fromName);
    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);/*from w w  w  .j  a  va2 s .c om*/
    }

    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.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 {//from ww w.ja v a  2s .co  m
        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());
    }
}

From source file:org.apache.kylin.common.util.MailService.java

/**
 * @param receivers/* w  ww . j a  v a2  s  .co  m*/
 * @param subject
 * @param content
 * @return true or false indicating whether the email was delivered successfully
 * @throws IOException
 */
public boolean sendMail(List<String> receivers, String subject, String content, boolean isHtmlMsg) {

    if (!enabled) {
        logger.info("Email service is disabled; this mail will not be delivered: " + subject);
        logger.info("To enable mail service, set 'kylin.job.notification-enabled=true' in kylin.properties");
        return false;
    }

    Email email = new HtmlEmail();
    email.setHostName(host);
    email.setStartTLSEnabled(starttlsEnabled);
    if (starttlsEnabled) {
        email.setSslSmtpPort(port);
    } else {
        email.setSmtpPort(Integer.valueOf(port));
    }

    if (username != null && username.trim().length() > 0) {
        email.setAuthentication(username, password);
    }

    //email.setDebug(true);
    try {
        for (String receiver : receivers) {
            email.addTo(receiver);
        }

        email.setFrom(sender);
        email.setSubject(subject);
        email.setCharset("UTF-8");
        if (isHtmlMsg) {
            ((HtmlEmail) email).setHtmlMsg(content);
        } else {
            ((HtmlEmail) email).setTextMsg(content);
        }
        email.send();
        email.getMailSession();

    } catch (EmailException e) {
        logger.error(e.getLocalizedMessage(), e);
        return false;
    }

    return true;
}

From source file:org.chenillekit.mail.services.impl.MailServiceImpl.java

private void setEmailStandardData(Email email) {
    email.setHostName(smtpServer);//  w  ww  .  ja va2 s  .com

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

    email.setDebug(smtpDebug);
    email.setSmtpPort(smtpPort);
    email.setSSL(smtpSSL);
    email.setSslSmtpPort(String.valueOf(smtpSslPort));
    email.setTLS(smtpTLS);
}

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

/**
 * Create data information to compose the mail
 *
 * @param hostName// ww  w  .  ja v  a2 s. co  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");
    }
}

From source file:org.graylog2.alerts.FormattedEmailAlertSender.java

private void sendEmail(String emailAddress, Stream stream, AlertCondition.CheckResult checkResult,
        List<Message> backlog) throws TransportConfigurationException, EmailException {
    LOG.debug("Sending mail to " + emailAddress);
    if (!configuration.isEnabled()) {
        throw new TransportConfigurationException(
                "Email transport is not enabled in server configuration file!");
    }//from w ww  .j  a  v a  2  s .co m

    final Email email = new SimpleEmail();
    email.setCharset(EmailConstants.UTF_8);

    if (isNullOrEmpty(configuration.getHostname())) {
        throw new TransportConfigurationException(
                "No hostname configured for email transport while trying to send alert email!");
    } else {
        email.setHostName(configuration.getHostname());
    }
    email.setSmtpPort(configuration.getPort());
    if (configuration.isUseSsl()) {
        email.setSslSmtpPort(Integer.toString(configuration.getPort()));
    }

    if (configuration.isUseAuth()) {
        email.setAuthenticator(new DefaultAuthenticator(Strings.nullToEmpty(configuration.getUsername()),
                Strings.nullToEmpty(configuration.getPassword())));
    }

    email.setSSLOnConnect(configuration.isUseSsl());
    email.setStartTLSEnabled(configuration.isUseTls());
    if (pluginConfig != null && !isNullOrEmpty(pluginConfig.getString("sender"))) {
        email.setFrom(pluginConfig.getString("sender"));
    } else {
        email.setFrom(configuration.getFromEmail());
    }
    email.setSubject(buildSubject(stream, checkResult, backlog));
    email.setMsg(buildBody(stream, checkResult, backlog));
    email.addTo(emailAddress);

    email.send();
}