Sending emails in Spring Boot with Amazon SES SMTP Interface
Tags: java, spring, aws, smtp
Published: 2021-11-06 03:20:39

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ionwqxf3g0a31qjpe3tm.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3xd3x9k3ak9trpb7lst.png)

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.

{% medium https://mmafrar.medium.com/setting-up-your-first-spring-boot-app-e3a42362677f %}

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'
}
```

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>
```

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.

{% gist https://gist.github.com/mmafrar/9e6db5a8b4ae3ee1ff45ee703c6be7eb %}

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.

{% gist https://gist.github.com/mmafrar/4c7bb94f418bd64b92d05c67400687b4 %}

Once you’re done with changes, enter [http://localhost:8080/api/v1/index] (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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kxbd4ijosgzbzf1mbuw.png)

___

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](https://dev.to/aws-builders/working-with-amazon-s3-presigned-urls-in-spring-boot-383n).

{% post https://dev.to/mmafrar/setting-up-your-first-spring-boot-app-8mk %}

Cover Image: Photo by [Anne Nygård](https://unsplash.com/@polarmermaid) on [Unsplash](https://unsplash.com)