Package com.elastisys.scale.commons.net.smtp

Source Code of com.elastisys.scale.commons.net.smtp.SmtpSender

package com.elastisys.scale.commons.net.smtp;

import static com.google.common.base.Preconditions.checkNotNull;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.elastisys.scale.commons.net.retryable.Requester;
import com.elastisys.scale.commons.net.retryable.RetryableRequest;

/**
* A {@link Requester} that sends an email message ({@link SmtpMessage}) to a
* certain SMTP server ({@link SmtpServerSettings}).
* <p/>
* The task will return <code>true</code> when the message is successfully sent.
* <p/>
* For details on JavaMail and security, refer to <a
* href="https://javamail.java.net/docs/SSLNOTES.txt">these notes</a>.
*
* @see RetryableRequest
*
*/
public class SmtpSender implements Requester<Boolean> {
  static final Logger LOG = LoggerFactory.getLogger(SmtpSender.class);

  /** The message to send. */
  private final SmtpMessage message;
  /** The SMTP server settings to use. */
  private final SmtpServerSettings serverSettings;

  /**
   * Constructs a new {@link SmtpSender} that will send an email message to a
   * given SMTP server.
   *
   * @param message
   *            The email message to send.
   * @param serverSettings
   *            The SMTP server settings to use.
   */
  public SmtpSender(SmtpMessage message, SmtpServerSettings serverSettings) {
    this.message = message;
    this.serverSettings = serverSettings;
  }

  @Override
  public Boolean call() throws Exception {
    sendMessage(this.message, this.serverSettings);
    return true;
  }

  private void sendMessage(SmtpMessage smtpMessage,
      SmtpServerSettings settings) throws EmailException {
    checkNotNull(this.message, "alert message cannot be null");
    if (LOG.isTraceEnabled()) {
      LOG.trace("sending email to {} with server settings {}",
          smtpMessage.getTo(), settings);
    }

    Email email = new SimpleEmail();
    email.setHostName(settings.getSmtpHost());
    email.setSmtpPort(settings.getSmtpPort());
    email.setFrom(smtpMessage.getFrom().toString());
    email.setSubject(smtpMessage.getSubject());
    email.setMsg(smtpMessage.getContent());
    email.setTo(smtpMessage.getTo());
    email.setSentDate(smtpMessage.getDateSent().toDate());
    if (settings.getAuthentication() != null) {
      email.setAuthentication(settings.getAuthentication().getUserName(),
          settings.getAuthentication().getPassword());
    }
    if (settings.isUseSsl()) {
      // enable the use of SSL for SMTP connections. NOTE: should
      // only be used for cases when the SMTP server port only supports
      // SSL connections (typically over port 465).
      email.setSSLOnConnect(true);
    } else {
      // Support use of the STARTTLS command (see RFC 2487 and RFC 3501)
      // to switch the connection to be secured by TLS for cases where the
      // server supports both SSL and non-SSL connections. This is
      // typically the case for most modern mail servers.
      email.setStartTLSEnabled(true);
    }
    email.setSocketConnectionTimeout(settings.getConnectionTimeout());
    email.setSocketTimeout(settings.getSocketTimeout());
    email.send();

    if (LOG.isTraceEnabled()) {
      LOG.trace("email sent to " + smtpMessage.getTo());
    }
  }

}
TOP

Related Classes of com.elastisys.scale.commons.net.smtp.SmtpSender

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.