Example usage for org.apache.commons.mail SimpleEmail setSocketTimeout

List of usage examples for org.apache.commons.mail SimpleEmail setSocketTimeout

Introduction

In this page you can find the example usage for org.apache.commons.mail SimpleEmail setSocketTimeout.

Prototype

public void setSocketTimeout(final int socketTimeout) 

Source Link

Document

Set the socket I/O timeout value in milliseconds.

Usage

From source file:com.adobe.acs.commons.hc.impl.SMTPMailServiceHealthCheck.java

@Override
@SuppressWarnings("squid:S1141")
public Result execute() {
    final FormattingResultLog resultLog = new FormattingResultLog();

    if (messageGatewayService == null) {
        resultLog.critical("MessageGatewayService OSGi service could not be found.");
        resultLog.info(//  w  w  w .j  ava  2 s . c om
                "Verify the Default Mail Service is active: http://<host>:<port>/system/console/components/com.day.cq.mailer.impl.CqMailingService");
    } else {
        final MessageGateway<SimpleEmail> messageGateway = messageGatewayService.getGateway(SimpleEmail.class);
        if (messageGateway == null) {
            resultLog.critical("The AEM Default Mail Service is INACTIVE, thus e-mails cannot be sent.");
            resultLog.info(
                    "Verify the Default Mail Service is active and configured: http://<host>:<port>/system/console/components/com.day.cq.mailer.DefaultMailService");
            log.warn("Could not retrieve a SimpleEmail Message Gateway");

        } else {
            try {
                List<InternetAddress> emailAddresses = new ArrayList<InternetAddress>();
                emailAddresses.add(new InternetAddress(this.toEmail));
                MailTemplate mailTemplate = new MailTemplate(IOUtils.toInputStream(MAIL_TEMPLATE),
                        CharEncoding.UTF_8);
                SimpleEmail email = mailTemplate.getEmail(StrLookup.mapLookup(Collections.emptyMap()),
                        SimpleEmail.class);

                email.setSubject("AEM E-mail Service Health Check");
                email.setTo(emailAddresses);

                email.setSocketConnectionTimeout(TIMEOUT);
                email.setSocketTimeout(TIMEOUT);
                try {
                    messageGateway.send(email);
                    resultLog.info(
                            "The E-mail Service appears to be working properly. Verify the health check e-mail was sent to [ {} ]",
                            this.toEmail);
                } catch (Exception e) {
                    resultLog.critical(
                            "Failed sending e-mail. Unable to send a test toEmail via the configured E-mail server: "
                                    + e.getMessage(),
                            e);
                    log.warn("Failed to send E-mail for E-mail Service health check", e);
                }

                logMailServiceConfig(resultLog, email);
            } catch (Exception e) {
                resultLog.healthCheckError(
                        "Sling Health check could not formulate a test toEmail: " + e.getMessage(), e);
                log.error("Unable to execute E-mail health check", e);
            }
        }
    }

    return new Result(resultLog);
}

From source file:org.sonar.plugins.emailnotifications.EmailNotificationChannel.java

private void send(EmailMessage emailMessage) throws EmailException {
    // Trick to correctly initilize javax.mail library
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

    try {/* w  w  w . j a v  a 2s .  c  om*/
        LOG.debug("Sending email: {}", emailMessage);
        String host = null;
        try {
            host = new URL(configuration.getServerBaseURL()).getHost();
        } catch (MalformedURLException e) {
            // ignore
        }

        SimpleEmail email = new SimpleEmail();
        if (StringUtils.isNotBlank(host)) {
            /*
            * Set headers for proper threading: GMail will not group messages, even if they have same subject, but don't have "In-Reply-To" and
            * "References" headers. TODO investigate threading in other clients like KMail, Thunderbird, Outlook
            */
            if (StringUtils.isNotEmpty(emailMessage.getMessageId())) {
                String messageId = "<" + emailMessage.getMessageId() + "@" + host + ">";
                email.addHeader(IN_REPLY_TO_HEADER, messageId);
                email.addHeader(REFERENCES_HEADER, messageId);
            }
            // Set headers for proper filtering
            email.addHeader(LIST_ID_HEADER, "Sonar <sonar." + host + ">");
            email.addHeader(LIST_ARCHIVE_HEADER, configuration.getServerBaseURL());
        }
        // Set general information
        email.setCharset("UTF-8");
        String from = StringUtils.isBlank(emailMessage.getFrom()) ? FROM_NAME_DEFAULT
                : emailMessage.getFrom() + " (Sonar)";
        email.setFrom(configuration.getFrom(), from);
        email.addTo(emailMessage.getTo(), " ");
        String subject = StringUtils.defaultIfBlank(StringUtils.trimToEmpty(configuration.getPrefix()) + " ",
                "") + StringUtils.defaultString(emailMessage.getSubject(), SUBJECT_DEFAULT);
        email.setSubject(subject);
        email.setMsg(emailMessage.getMessage());
        // Send
        email.setHostName(configuration.getSmtpHost());
        if (StringUtils.equalsIgnoreCase(configuration.getSecureConnection(), "SSL")) {
            email.setSSL(true);
            email.setSslSmtpPort(String.valueOf(configuration.getSmtpPort()));

            // this port is not used except in EmailException message, that's why it's set with the same value than SSL port.
            // It prevents from getting bad message.
            email.setSmtpPort(configuration.getSmtpPort());
        } else if (StringUtils.isBlank(configuration.getSecureConnection())) {
            email.setSmtpPort(configuration.getSmtpPort());
        } else {
            throw new SonarException(
                    "Unknown type of SMTP secure connection: " + configuration.getSecureConnection());
        }
        if (StringUtils.isNotBlank(configuration.getSmtpUsername())
                || StringUtils.isNotBlank(configuration.getSmtpPassword())) {
            email.setAuthentication(configuration.getSmtpUsername(), configuration.getSmtpPassword());
        }
        email.setSocketConnectionTimeout(SOCKET_TIMEOUT);
        email.setSocketTimeout(SOCKET_TIMEOUT);
        email.send();

    } finally {
        Thread.currentThread().setContextClassLoader(classloader);
    }
}

From source file:org.sonar.server.notification.email.EmailNotificationChannel.java

private void send(EmailMessage emailMessage) throws EmailException {
    // Trick to correctly initialize javax.mail library
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

    try {/*w ww . ja  v  a 2 s .  co m*/
        LOG.debug("Sending email: {}", emailMessage);
        String host = null;
        try {
            host = new URL(configuration.getServerBaseURL()).getHost();
        } catch (MalformedURLException e) {
            // ignore
        }

        SimpleEmail email = new SimpleEmail();
        if (StringUtils.isNotBlank(host)) {
            /*
             * Set headers for proper threading: GMail will not group messages, even if they have same subject, but don't have "In-Reply-To" and
             * "References" headers. TODO investigate threading in other clients like KMail, Thunderbird, Outlook
             */
            if (StringUtils.isNotEmpty(emailMessage.getMessageId())) {
                String messageId = "<" + emailMessage.getMessageId() + "@" + host + ">";
                email.addHeader(IN_REPLY_TO_HEADER, messageId);
                email.addHeader(REFERENCES_HEADER, messageId);
            }
            // Set headers for proper filtering
            email.addHeader(LIST_ID_HEADER, "SonarQube <sonar." + host + ">");
            email.addHeader(LIST_ARCHIVE_HEADER, configuration.getServerBaseURL());
        }
        // Set general information
        email.setCharset("UTF-8");
        String from = StringUtils.isBlank(emailMessage.getFrom()) ? FROM_NAME_DEFAULT
                : (emailMessage.getFrom() + " (SonarQube)");
        email.setFrom(configuration.getFrom(), from);
        email.addTo(emailMessage.getTo(), " ");
        String subject = StringUtils.defaultIfBlank(StringUtils.trimToEmpty(configuration.getPrefix()) + " ",
                "") + StringUtils.defaultString(emailMessage.getSubject(), SUBJECT_DEFAULT);
        email.setSubject(subject);
        email.setMsg(emailMessage.getMessage());
        // Send
        email.setHostName(configuration.getSmtpHost());
        configureSecureConnection(email);
        if (StringUtils.isNotBlank(configuration.getSmtpUsername())
                || StringUtils.isNotBlank(configuration.getSmtpPassword())) {
            email.setAuthentication(configuration.getSmtpUsername(), configuration.getSmtpPassword());
        }
        email.setSocketConnectionTimeout(SOCKET_TIMEOUT);
        email.setSocketTimeout(SOCKET_TIMEOUT);
        email.send();

    } finally {
        Thread.currentThread().setContextClassLoader(classloader);
    }
}