Sending E-mail from TomEE Using Amazon Simple Email Service (SES)

If you are running a web application in Amazon EC2, then SES is a great option for an outgoing SMTP mail server.

First, let’s evaluate all of your options for an SMTP server:

  1. Corporate SMTP server – This option is applicable for organizations that already have a mail server running. If this server allows access to it from within the corporate network then accessing from an EC2 instance can become difficult.
  2. Gmail or Google Apps like services – Gmail or Google Apps like services are very good options for startups. With this, you can use Google’s SMTP server to send email. There is only one problem with this approach that I can find. Google always sets the “From:” field to the account used to authenticate with the SMTP server. That means, you can use only one from address. This may be OK with your application.
  3. Amazon Simple Email Service – SES is a relaying only SMTP server. It has many checks and balances in place to make relaying safe. SES allows you set any From: address as long as these addresses have been validated. If your web application is running in EC2, you get 2000 free e-mails a day. That should be plenty for a startup.

Getting Setup

Log into AWS console and then go to the SES service.

First thing you need to do is verify the domain that is used for the From: addresses.

image

Click Domains and then Verify a New Domain.

Enter the domain name, such as example.com. This will generate a TXT DNS record. You can manually add this record or if you are using Amazon S3 as your DNS server, you can have AWS console automatically add it. Details are here.

Once you validate a domain, you can send emails using any From address that belongs to that domain. However, that is only true in production mode. While you are still in sandbox mode, all recipient email addresses need  to be validated. So, click on Email Addresses and validate all recipient emails that you will use during testing.

Finally, you need to generate the user name and password to be used for SMTP authentication.

Click SMTP Settings. Click Create My SMTP Credentials. The console will create a new user with AWS Identity and Access manager (IAM). Once the user is created, system will also assign a SMTP user name and password for the IAM user. Note down these credentials or download it. That’s because, AWS does not save the information anywhere. (This is for your protection so that no one can log into your AWS account and get the credentials).

Configuring TomEE

Open conf/tomee.xml and add these lines:

<Resource id="mail/Default" type="javax.mail.Session">
  mail.transport.protocol=smtp
  mail.smtp.host=email-smtp.us-east-1.amazonaws.com
  mail.smtp.port=587
  mail.smtp.auth=true
  mail.smtp.user=SES_SMTP_USERNAME
  mail.smtp.password=SES_SMTP_PASSWORD
  mail.smtp.starttls.enable=true
  mail.smtp.starttls.required=true
  user=SES_SMTP_USERNAME
  password=SES_SMTP_PASSWORD
</Resource>

Note, the user name and password needs to be entered twice.

Now, you can access the mail session resource.

public class MailManager {
@Resource(name = "mail/Default")
private Session mailSession;

}

That’s all there is to it!

One thought on “Sending E-mail from TomEE Using Amazon Simple Email Service (SES)

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.