Amazon SES- Sending emails with your Spring boot application

Aniruddh Fataniya
AWS Tip
Published in
4 min readOct 25, 2022

--

Sending promotional or transactional emails is now a key part of business applications.

Amazon Web Services (AWS) provide an email service called Amazon Simple Email Service (Amazon SES) for sending emails. You can use AWS SDK to use this service in your Spring boot application.

This article will show how to use this service to send emails from a Spring boot application.

The first thing you have to do is to add email identities to Amazon SES. It will only allow the registered email identities to send the emails. By default, your Amazon SES will be working on a sandbox environment. So you can only send the emails to the registered email identities. You can request AWS support to remove the sandbox environment.

Let’s create an email identity. First, login into the AWS management console and search Simple Email Service or SES.

You will be redirected to the Verified identities dashboard, once you click on it. Create an email identity by clicking on Create identity.

On the Create identity dashboard, select Email address under the Identity type and enter the email address and click on Create identity.

You will receive a verification email on the email address you entered. Once you click on the verification link, an email identity will be created. Now you can send and receive emails on this verified email identity.

The next part is to create a spring boot project and configure the AWS credentials of the IAM user having AmazonSESFullAccess permission.

For that, in the terminal, run the following command the configure the credentials and add the credentials.

aws configure

Make sure to enter the region in which you are working with Amazon SES.

Now add the following code to the respective files.

In the pom.xml, add the dependency management for AWS SDK and the dependency of SES in the <dependencies> section, and also add the other required dependencies required by your project.

<!-- AWS SDK -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.17.282</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--aws SES-->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ses</artifactId>
</dependency>
</dependencies>

Create a class called Email as follows having the following attributes with getter/setters and with constructors:

public class Email {

private String sender;

private String receiver;

private String subject;

private String emailBody;
}

In the service interface, add the following function:

void sendEmail(SesClient client, Email email) throws MessagingException;

In the implementation file of the service, add the body of the function as follows:

@Override
public void sendEmail(SesClient client, Email email) throws MessagingException {

Destination destination = Destination.builder()
.toAddresses(email.getReceiver())
.build();

Content content = Content.builder()
.data(email.getEmailBody())
.build();

Content subject = Content.builder()
.data(email.getSubject())
.build();

Body body = Body.builder()
.html(content)
.build();

Message message = Message.builder()
.subject(subject)
.body(body)
.build();

SendEmailRequest emailRequest = SendEmailRequest.builder()
.message(message)
.source(email.getSender())
.destination(destination)
.build();

try {
client.sendEmail(emailRequest);

} catch (SesException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}

Also, create an SES client in the controller file, you can also create a separate configuration file for the same. Replace the YOUR_REGION with the region you are working on SES.

Region region = Region.YOUR_REGION;
SesClient sesClient = SesClient.builder()
.region(region)
.credentialsProvider(ProfileCredentialsProvider.create())
.build();

Now in the controller class, create an endpoint for this sending the email.

@Autowired
private SESService service;
@PostMapping("/simpleEmail")
ResponseEntity<Email> sendEmail(@RequestBody Email email) throws MessagingException {
service.sendEmail(sesClient,email);
return new ResponseEntity<>(HttpStatus.OK);
}

Now you can test this endpoint using some API platforms like Postman.

If this post was helpful, please do follow and click the clap 👏 button below to show your support.

Thank you for reading!

--

--

AWS Professional | AWS Community Builder | AWS certified solution architect - professional | Cloud Engineer at Rishabh Software