AWS SES Integration for Java Applications
Use Case
I came across a use-case, where I had to integrate Amazon Simple Email Service (SES) to send transactional emails from a JAVA application using my own registered domain. This blog will guide the reader through the basic steps required for setting up or integrating Amazon SES with my JAVA application.
About Amazon SES
It is an email service built and maintained by Amazon. It has cost efficient, reliable and scalable infrastructure ready to serve its customers. It can be used to send transactional email, marketing messages, or any other type of high-quality content to the customers. It can also be used to receive messages and deliver them to an Amazon S3 bucket, call your custom code via an AWS Lambda function, or publish notifications to Amazon SNS.
Requirements
1. AWS Account with full access to Simple Email Service (SES).
2. Registered domain to be used for sending email.
3. Working Java application for sending emails.
Setting Up Amazon Simple Email Service (SES)
To start the setup we first need to have a domain from which we can send emails. Following sample domain was created:
Now, we have a sample domain, sample.videoready.tv. Now we need to verify it so that we can setup Amazon Simple Email Service (SES) for this domain.
Following are the steps to setup SES
1. Go to SES console
2. On the left we have an option for Domain, click on the domain option and then select Verify a New Domain:
3. Enter the name of created domain and select the box to Generate DKIM Settings. The domain is not verified util the records generated in this process are entered in DNS, in my case it is AWS Route 53. Following records will be generated:
It gives an option to setup all records in AWS Route53. But I do not need to set the MX records, but in my use-case, I only needed to send emails and not receive any on my domain. So we setup only TXT and DKIM records in AWS Route53.
After these records have been setup. The domain takes some time to be verified:
4. After the domain verification, we can send a test email from the SES console, to verify that it is functioning properly:
Now that we have verified that the Amazon SES service is functioning properly. We move on to integrating this with the Java application. To integrate it with a java application we use SMTP setting and then with AWS JAVA SDK. The SMTP settings generate SMTP credentials, which are setup in the application code. These credentials allow the application to use Amazon SES and use the verified domain to send mails.
Setting Amazon SES for SMTP
On the left, we have an option SMTP Setting, click on it. Then select Generate My SMTP Credentials:
Following credentials are generated, which need to be configured in the Java Application. So that application is authorized to use SES for sending emails:
In the Java Application, we define SMTP_USERNAME and SMTP_PASSWORD, which authorizes the access to Amazon SES. Apart from SMTP credentials we set the sender and the receiver of the mail. Here, we also define the content and subject of the mail:
Setting Amazon SES with AWS JAVA SDK
If using AWS JAVA SDK to send emails, we can use AWS user secret key and access key to authorize Java application to send emails using Amazon SES. After installing the SDK we setup the keys as follows, to allow the access of application on Amazon SES:
Now, the Java application’s integration with Amazon SES is complete, and emails can be sent using both SMTP as well as AWS JAVA SDK method. Using Amazon SES is good in such use-case because if the application becomes heavy and it needs to send a large number of emails. The large volume can easily be handled by Amazon SES due to its scalable architecture.
Hope this blog was of help to you.