DEV Community

Cover image for Sending emails in Spring Boot with Amazon SES SMTP Interface
Afrar Malakooth for AWS Community Builders

Posted on • Updated on • Originally published at Medium

Sending emails in Spring Boot with Amazon SES SMTP Interface

Sending out emails for features like password reset, email notifications etc. has become an integral part of every software being developed today. Most common way is to use a SMTP client, in this story we’ll look into Amazon SES.

Welcome to another Spring Boot tutorial. For this tutorial I’ll be assuming that you have a basic understanding of Spring Boot and AWS. Let’s see how we can utilize an AWS service for sending emails.


Go ahead and create an AWS account if you haven’t got one already. Once you have logged into AWS Management Console, type Amazon Simple Email Service on the search bar and click on the first result. As per the time of writing this story you should be landing on a similar page shown below.

SES Home page

Make sure to select your preferred AWS Region from the highlighted dropdown on top right hand corner. Once done, click on SMTP Settings highlighted in red from the left hand navigation pane. Take a note of Server Name provided in the SMTP Settings page. To send emails using the Amazon SES SMTP Interface, you first have to create a set of SMTP credentials. Click on Create My SMTP Credentials button.

Create User for SMTP page

Above form lets you create an IAM user for SMTP authentication with Amazon SES. Enter the name of a new IAM user or accept the default and click Create to set up your SMTP credentials. Make sure to take a note of username and password as we’ll be needing it later. After user is created navigate back to SES Home page.

For development purposes all the email addresses we’ll be using for sending and receiving email should be verified beforehand. Click on Email Addresses tab under Identity Management from the left hand navigation pane. Click on the Verify A New Email Address button and make sure get at least one email address verified. A verification email will be sent to the email address you entered.

In order to proceed further with this tutorial I’ll be using the Spring Boot app created in above Medium story. If you’re already having project to work, you’re good to go. Open the build.gradle file and add the highlighed dependancy, if you’re using Maven do the equivalent action.

dependencies {
 implementation 'org.springframework.boot:spring-boot-starter-web'
 implementation 'org.springframework.boot:spring-boot-starter-mail'
}
Enter fullscreen mode Exit fullscreen mode

Next create application.properties file inside src/main/resources folder and add the below text. Make sure to replace placeholders with relevant values. Email address you are using here should have to be verified from SES Management Console.

# Email configuration
spring.mail.host=email-smtp.us-east-1.amazonaws.com
spring.mail.username=<YOUR_SMTP_USERNAME>
spring.mail.password=<YOUR_SMTP_PASSWORD>
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.port=587
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
from.email.address=<YOUR_EMAIL_ADDRESS>
Enter fullscreen mode Exit fullscreen mode

Then create a Java class called EmailService.java inside the root package and add the below code referring to the Gist. This will be the generic class which we’ll be using to send out the emails.

Next make sure to update DefaultController.java class created in the previous tutorial to match the below given Gist. Make sure to replace placeholders with relevant values. Email address you are using here should have to be verified from SES Management Console.

Once you’re done with changes, enter http://localhost:8080/api/v1/index in your web browser and navigate to the address. This will invoke the email sending method and you’ll have the email delivered to your inbox.

Test email delivered to inbox


Happy Coding! Below is a DEV Community video I have published and if you’re interested check out my DEV Community post on Working with Amazon S3 presigned URLs in Spring Boot.

Cover Image: Photo by Anne Nygård on Unsplash

Top comments (0)