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. If you’re totally new to Spring Boot check out my starter tutorial Setting up your first Spring Boot app on Medium. 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.
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.
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 YouTube video. 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.
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
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.