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
AWS
places your account in aSandbox
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. - 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 Email
will send emails using theSMTP
endpoint ofAWS SES
. For this, you need to create and save yourAWS 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
- You can download the
Apache Commons Email
binaries from here: http://commons.apache.org/proper/commons-email/download_email.cgi - You will need the
commons-email-.jar JAR
from the unzipped binaries - Place this
JAR
file in yourCLASSPATH
(typically the~/WEB-INF/lib
directory in a standardJava WAR file
) AWS SES
requires a secure connection to send email via itsSMTP endpoint
as described here: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-connect.htmlApache Commons Email
supportsSTARTTLS
and the example below covers this- The
AWS SES
specific settings are:SMTP Hostname
- This is the domain name of the
AWS SES SMTP server
closest to you - The current
AWS SES SMTP endpoints
are 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
username
that you acquired in step 4 above
- This is the
SMTP Server Password
- This is the
password
that you acquired in step 4 above
- This is the
- Enable
SSL
on connect - Enable
STARTTLS
- By default
Apache Commons Email
will connect to theAWS SES SMTP endpoint
on port587
which 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