Example usage for org.apache.commons.mail ImageHtmlEmail setSocketConnectionTimeout

List of usage examples for org.apache.commons.mail ImageHtmlEmail setSocketConnectionTimeout

Introduction

In this page you can find the example usage for org.apache.commons.mail ImageHtmlEmail setSocketConnectionTimeout.

Prototype

public void setSocketConnectionTimeout(final int socketConnectionTimeout) 

Source Link

Document

Set the socket connection timeout value in milliseconds.

Usage

From source file:org.apache.isis.core.runtime.services.email.EmailServiceDefault.java

@Override
public boolean send(final List<String> toList, final List<String> ccList, final List<String> bccList,
        final String subject, final String body, final DataSource... attachments) {

    try {//from   w  ww  .ja  va 2s.c o m
        final ImageHtmlEmail email = new ImageHtmlEmail();

        final String senderEmailAddress = getSenderEmailAddress();
        final String senderEmailPassword = getSenderEmailPassword();
        final String senderEmailHostName = getSenderEmailHostName();
        final Integer senderEmailPort = getSenderEmailPort();
        final Boolean senderEmailTlsEnabled = getSenderEmailTlsEnabled();
        final int socketTimeout = getSocketTimeout();
        final int socketConnectionTimeout = getSocketConnectionTimeout();

        email.setAuthenticator(new DefaultAuthenticator(senderEmailAddress, senderEmailPassword));
        email.setHostName(senderEmailHostName);
        email.setSmtpPort(senderEmailPort);
        email.setStartTLSEnabled(senderEmailTlsEnabled);
        email.setDataSourceResolver(new DataSourceClassPathResolver("/", true));

        email.setSocketTimeout(socketTimeout);
        email.setSocketConnectionTimeout(socketConnectionTimeout);

        final Properties properties = email.getMailSession().getProperties();

        properties.put("mail.smtps.auth", "true");
        properties.put("mail.debug", "true");
        properties.put("mail.smtps.port", "" + senderEmailPort);
        properties.put("mail.smtps.socketFactory.port", "" + senderEmailPort);
        properties.put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtps.socketFactory.fallback", "false");
        properties.put("mail.smtp.starttls.enable", "" + senderEmailTlsEnabled);

        email.setFrom(senderEmailAddress);

        email.setSubject(subject);
        email.setHtmlMsg(body);

        if (attachments != null && attachments.length > 0) {
            for (DataSource attachment : attachments) {
                email.attach(attachment, attachment.getName(), "");
            }
        }

        final String overrideTo = getEmailOverrideTo();
        final String overrideCc = getEmailOverrideCc();
        final String overrideBcc = getEmailOverrideBcc();

        final String[] toListElseOverride = actually(toList, overrideTo);
        if (notEmpty(toListElseOverride)) {
            email.addTo(toListElseOverride);
        }
        final String[] ccListElseOverride = actually(ccList, overrideCc);
        if (notEmpty(ccListElseOverride)) {
            email.addCc(ccListElseOverride);
        }
        final String[] bccListElseOverride = actually(bccList, overrideBcc);
        if (notEmpty(bccListElseOverride)) {
            email.addBcc(bccListElseOverride);
        }

        email.send();

    } catch (EmailException ex) {
        LOG.error("An error occurred while trying to send an email", ex);
        final Boolean throwExceptionOnFail = isThrowExceptionOnFail();
        if (throwExceptionOnFail) {
            throw new EmailServiceException(ex);
        }
        return false;
    }

    return true;
}