Amazon Simple Email Service (SES) is a cloud-based email sending service that allows you to add email functionality to any application. Using Amazon SES, you send transactional emails, marketing messages, or any other kind of notification emails to your customers. Built on the reliable and scalable infrastructure of Amazon Web Services (AWS), it is a cost-effective service for companies of all sizes that use email to communicate with their customers.
Want to integrate AWS SES in Node.js application? Follow this tutorial for step-by-step instructions.
I have been using Amazon SES for sending different kinds of emails for my projects including order confirmations, welcome emails, occasional announcements, and even for weekly newsletters. In this tutorial, I will teach you how to setup Amazon SES in a Spring Boot project and how to scale it to support hundreds of emails per minute.
Why Amazon SES?
There are number of factors why I chose Amazon SES over other email service providers for integrating emails in my web apps. First of all, it is a cost-effective solution for indie makers who quickly want to launch multiple MVPs (minimal viable products) just to validate their ideas. It only costs $0.10 per thousand email messages. Since it is a pay as you go service, so you only pay for what you use. There are no upfront fees, no minimum charges, and no fixed monthly expenses. Moreover, if your application is hosted in Amazon EC2, the first 62,000 you send every month are free of cost. Since all of my applications are running on Amazon EC2 instance, this free limit was more than enough for sending transactional and marketing emails at the beginning.
Secondly, it is a reliable and scalable service with higher deliverability, thanks to Amazon Web Services multiple datacenters and redundant systems. When I first integrated Amazon SES in StartupBase, I created a special package in the application on top of AWS Java SDK. For new Spring Boot projects, I just need to copy this package into the source code and That’s it! I do not need to write everything from scratch.
In the following sections, I will discuss in details how you can add this package to your web application with code examples.
Setup Amazon SES
To use Amazon SES service in your web project, you must have an AWS account with full access to Simple Email Service (SES). If you are not already signed up, click here to create a new AWS account. The new users are entitled to get 12 months of free tier access to gain free, hand-on experience with the AWS platform, products, and services.
In order to setup Amazon SES for your custom domain, you must have permissions to edit DNS records of that domain name. If you do not have access to DNS records, contact your domain name registrar to get that. In this tutorial, I am going to use a dummy domain name mysestest.com.
Now go to your Amazon SES console, found under Services tab in top bar menu. The SES console looks like following:
In the left navigation pane of the Amazon SES console, under Identity Management, click on Domain. To verify a new domain, click on Verify a New Domain button on top left.
Enter the name of your domain in the box (for this tutorial, I entered mysestest.com) and select Generate DKIM Settings. Afterwards, click on Verify This Domain to generate verification and DKIM records as shown below.
Now go to your DNS service provider (mostly domain name registrar) and add all required DNS records (TXT, CNAME and MX) to complete the domain verification step. MX records are only required if you want to receive emails via SES. For just sending emails, you can ignore these records. Once these records have been setup, it takes some time to complete the verification.
Once the verification is completed, you will receive a confirmation email from Amazon Web Services. Then you are ready to proceed to next step: integration of Amazon SES in your web application using AWS Java SDK.
Amazon SES integration with Spring Boot
Now that we have verified Amazon SES for our domain mysestest.com, we move on to integrating this with our Spring Boot web application. If you want to create your own Spring Boot-based project, go to Spring Initializr and create a new Gradle project with Java and Spring Boot. With Amazon SES, you can send an email in three ways: using the console, using the SMTP interface, or using the API. This tutorial uses Amazon SES API to send emails programmatically. Before you use API to send emails, you need to create AWS access keys. For more information, please check getting AWS access keys.
We will build a simple Spring Boot application and add Amazon SES to it. For this guide, I assume that you are familiar with the tools required to run a Spring Boot application. At minimum, you need JDK 1.8 or later to compile the source code, a text editor or IDE to edit the files and Gradle 4+ for building web application. Although you can use any build system you like to build Spring Boot apps, for this guide, I am going to use Gradle. If you are not familiar with it, refer to Building Java Projects with Gradle.
Create a directory structure
First create a project directory
ses-spring-boot in the file system and then create the following sub-directory structure inside. You can do both in one command
mkdir -p ses-spring-boot/src/main/java/app/ on *nix systems.
└── src └── main └── java └── app
Create a Gradle build file
gradle.build file in project root directory and copy the following Groovy code into it.
Create Amazon SES classes
Now we are ready to add Amazon SES classes to our web application. Create a new folder
app folder and add the following classes:
SESForm.java– List all your possible From email addresses in this enum class.
AmazonAttachment.java– This classes is used to store attachment files if any.
AmazonEmail.java– It stores the email details like To/From email addresses, subject, email body etc.
SESProcessor.java– This is the entry class which runs in the background as a thread. It initializes
SESWorker.javathreads for sending emails and provides methods to add new
SESEmail.javaobjects into the waiting queue. It also performs load balancing among worker threads.
SESWorker.java– This is where we send emails programmatically using Amazon SES API. Each worker is a separate thread with own queue of email messages. You can decide how many workers you want to initialize in
MAX_WORKERSvariable depending on your requirements.
SESWorker.java class loads AWS access keys from classpath at line 52 to initialize SES client. To authorize Spring Boot application to send emails using Amazon SES, create
AwsCredentials.properties file in
src/main/resources/ folder which should include user secret key and access key as shown below:
Create web controller and application classes
Now you can create a simple web controller (
AppController.java) for handling HTTP requests in
After this, it is time to create an application class in
app folder that includes the
main() method to launch the Spring Boot application. This classes is named
Run the application
To run the application, execute the following command:
$ ./gradlew build && java -jar build/libs/ses-spring-boot-0.0.1-SNAPSHOT.jar
At this point, our Amazon SES integration with Spring Boot is completed and emails can be sent using AWS Java SDK API. The
index() method of
AppController.java class contains two examples of how you can use
SESProcessor.java to send emails from anywhere in the application. The complete source code of this tutorial is available on GitHub. You can download it from here.
Source code: You can download the complete source code from GitHub available under MIT license.
In this tutorial, we learned how to use Amazon SES to send emails in Spring Boot application. Amazon SES is the cheapest service for sending large number of transactional, marketing, and notification emails to the customers. It is a reliable service built on top of Amazon Web Services infrastructure. It scales well when the application grows bigger and requires to send a large number of emails.
I hope this blog post was helpful to you to understand what is Amazon SES and how it can be used in Java applications for email communication.