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
- You need to sign-up for an AWS account. Click on the Sign up link here: http://aws.amazon.com/ses/
- Initially
AWSplaces your account in aSandboxso 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. - 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
Apache Commons Emailwill send emails using theSMTPendpoint ofAWS SES. For this, you need to create and save yourAWS SES SMTP credentialsby 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
- You can download the
Apache Commons Emailbinaries from here: http://commons.apache.org/proper/commons-email/download_email.cgi - You will need the
commons-email-.jar JARfrom the unzipped binaries - Place this
JARfile in yourCLASSPATH(typically the~/WEB-INF/libdirectory in a standardJava WAR file) AWS SESrequires a secure connection to send email via itsSMTP endpointas described here: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-connect.htmlApache Commons EmailsupportsSTARTTLSand the example below covers this- The
AWS SESspecific settings are:SMTP Hostname- This is the domain name of the
AWS SES SMTP serverclosest to you - The current
AWS SES SMTP endpointsare available here: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-connect.html
- This is the domain name of the
SMTP Server Username- This is the
usernamethat you acquired in step 4 above
- This is the
SMTP Server Password- This is the
passwordthat you acquired in step 4 above
- This is the
- Enable
SSLon connect - Enable
STARTTLS- By default
Apache Commons Emailwill connect to theAWS SES SMTP endpointon port587which is supported byAWS SES
- By default
- 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