Sending emails through AWS SES using Apache Commons Email

This post covers the setttings required to send emails through Amazon Web Sevice's Simple Email Service SMTP endpoint using Apache Commons Email.

Apache Commons Email provides an API for sending email. It is built on top of, and simplifies, the Java Mail API.

More information on Apache Commons Email is available here: http://commons.apache.org/proper/commons-email/

AWS SES is an outbound-only email-sending service provided by Amazon Web Services.

More information on AWS SES is available here: http://aws.amazon.com/ses/

Setup your AWS SES Account

Follow the AWS SES Quickstart Guide to get setup and configure your AWS SES account: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/quick-start.html

  1. You need to sign-up for an AWS account. Click on the Sign up link here: http://aws.amazon.com/ses/
  2. Initially AWS places your account in a Sandbox so you can test the service. In the sandbox you can send a total of 200 emails per 24 hour period, at a maximum rate of 1 email per second.
  3. In the sandbox you can send emails to and from verified individual email addresses or domains only. Verify your email addresses and/or domains by following these instructions: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-addresses-and-domains.html
  4. Apache Commons Email will send emails using the SMTP endpoint of AWS SES. For this, you need to create and save your AWS SES SMTP credentials by following the steps here: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html

Thats it! AWS SES is setup.

You can send test emails by following the steps here: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/getting-started-send-from-console.html

Use Apache Commons Email to send Email

  1. You can download the Apache Commons Email binaries from here: http://commons.apache.org/proper/commons-email/download_email.cgi
  2. You will need the commons-email-.jar JAR from the unzipped binaries
  3. Place this JAR file in your CLASSPATH (typically the ~/WEB-INF/lib directory in a standard Java WAR file)
  4. AWS SES requires a secure connection to send email via its SMTP endpoint as described here: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-connect.html
  5. Apache Commons Email supports STARTTLS and the example below covers this
  6. The AWS SES specific settings are:
    • SMTP Hostname
    • SMTP Server Username
      • This is the username that you acquired in step 4 above
    • SMTP Server Password
      • This is the password that you acquired in step 4 above
    • Enable SSL on connect
    • Enable STARTTLS
      • By default Apache Commons Email will connect to the AWS SES SMTP endpoint on port 587 which is supported by AWS SES
  7. Here is the sample code
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

Email email = new SimpleEmail();
email.setHostName("");
email.setAuthenticator(new DefaultAuthenticator("", ""));
email.setSSLOnConnect(true);
email.setStartTLSEnabled(true);

try {
    email.setFrom("");
    email.setSubject("");
    email.setMsg("");
    email.addTo("");
    email.send();
} catch (EmailException e) {
    e.printStackTrace();
}

Requesting Production Access to AWS SES

Once you have tested SES, you can request production access by following these instructions: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/request-production-access.html