class EmailDispatcher implements NotificationDispatcher {

  @Override
  public void dispatch(Notification notification) {
    LOG.info("Sending email: {}", notification);

    if (null == notification.DispatchProperties) {
      LOG.error("Unable to dispatch an email notification that does not contain SMTP properties");

      if (null != notification.Callback) {
        notification.Callback.onFailure(notification.CallbackIds);
      }

      return;
    }

    // convert properties to JavaMail properties, extracting certain properties for use later
    String fromAddress = null;
    Properties properties = new Properties();
    for (Entry<String, String> entry : notification.DispatchProperties.entrySet()) {
      String key = entry.getKey();
      String value = entry.getValue();

      properties.put(key, value);

      // this is needed later on for RFC 2822 compliance when setting headers
      if (key.equals(JAVAMAIL_FROM_PROPERTY)) {
        fromAddress = value;
      }
    }

    // notifications must have recipients
    if (null == notification.Recipients) {
      LOG.error("Unable to dispatch an email notification that does not have recipients");

      if (null != notification.Callback) {
        notification.Callback.onFailure(notification.CallbackIds);
      }

      return;
    }

    // create a simple email authentication for username/password
    final Session session;
    EmailAuthenticator authenticator = null;

    if (null != notification.Credentials) {
      authenticator = new EmailAuthenticator(notification.Credentials);
    }

    session = Session.getInstance(properties, authenticator);

    try {
      // !!! at some point in the future we can worry about multipart
      MimeMessage message = new MimeMessage(session);

      for (Recipient recipient : notification.Recipients) {
        InternetAddress address = new InternetAddress(recipient.Identifier);
        message.addRecipient(RecipientType.TO, address);
      }

      message.setSentDate(new Date());
      message.setSubject(notification.Subject);
      message.setText(notification.Body, "UTF-8", "html");

      // RFC 2822 compliance
      if (null != fromAddress) {
        message.setFrom(fromAddress);
      }

      Transport.send(message);

      if (LOG.isDebugEnabled()) {
        LOG.debug("Successfully dispatched email to {}",
            notification.Recipients);
      }

      // callback to inform the interested parties about the successful dispatch
      if (null != notification.Callback) {
        notification.Callback.onSuccess(notification.CallbackIds);
      }
    } catch (Exception exception) {
      LOG.error("Unable to dispatch notification via Email", exception);

      // callback failure
      if (null != notification.Callback) {
        notification.Callback.onFailure(notification.CallbackIds);
      }
    } finally {
      try {
        session.getTransport().close();
      } catch (MessagingException me) {
        LOG.warn("Dispatcher unable to close SMTP transport", me);
      }
    }
  }

}