In this post, we’ll explain step by step how to send templated emails with Amazon SES.
· Prerequisites
· Overview
∘ What is Amazon SES?
∘ Why use Amazon SES?
· Setting Up Amazon SES
∘ Creating and verifying identities in Amazon SES
∘ Create email template
· Spring Boot App Setup
∘ Configuring Beans SesV2Client
∘ Sending Email Template
· Test the REST API:
· Conclusion
· References
Prerequisites
This is the list of all the prerequisites:
- Java 17
- Starter Boot 3.X.X
- Maven 3.6.3
- An active AWS account.
- AWS CLI to perform tasks related to Amazon SES templates.
Overview
What is Amazon SES?
Amazon SES is a cloud-based email service provider that can integrate into any application for high-volume email automation. It allows customers to work confidently without an on-premises Simple Mail Transfer Protocol (SMTP) mail server using the Amazon SES API or SMTP interface.
Why use Amazon SES?
- Easy to use
- You pay only for what you use.
- Supports a variety of deployments including dedicated, shared, or owned IP addresses
- Provides reports on sender statistics and email deliverability.
- Content Personalization.
- It is a great way to advertise, send newsletters, special offers, and any other type of high-quality content.
Setting Up Amazon SES
We can send an email with Amazon Simple Email Service (Amazon SES) using the Amazon SES console, the Amazon SES Simple Mail Transfer Protocol (SMTP) interface, or the Amazon SES API. In this story, we’ll use the AWS SDKs to make the API for Amazon SES easier to access.
Creating and verifying identities in Amazon SES
- Log in to the AWS Management Console and open the Amazon Simple Email Service.

When you start using Amazon SES, your AWS account is in the sandbox. To help prevent fraud and abuse, and to help protect your reputation as a sender, AWS applies certain restrictions to new Amazon SES accounts. If your SES account is in a sandbox environment then you would be able to send emails only to verified identities only.
Let’s verify identities to send emails.

We added the email address for verification. Verifying an identity with Amazon SES confirms that you own it and helps prevent unauthorized use.
After you’ve created your email address identity, you must complete the verification process.
To verify an email address identity
- Check the inbox of the email address used to create your identity and look for an email from no-reply-aws@amazon.com.
- Open the email and click the link to complete the verification process for the email address. After it’s complete, the Identity status updates to Verified.


Note: Production access can be requested and will disable the need for mail address verification.
Create email template
Email templates enable you to send personalized email to one or more destinations in a single operation. Creating, updating, and deleting templates is only supported on CLI and SDK.
These templates include a subject line and the text and HTML parts of the email body. The subject and body sections may also contain unique values that are personalized for each recipient.
Below is the template we want to create:

Create a new file with JSON extension.
{
"Template": {
"TemplateName": "NewsletterTemplate",
"SubjectPart": "Welcome to AWS SES demo !!",
"HtmlPart": " <table width='100%' bgcolor='#e0e0e0' cellpadding='0' cellspacing='0' border='0'> <tr><td> <table align='center' width='100%' border='0' cellpadding='0' cellspacing='0' style='max-width:650px; background-color:#fff; font-family:Verdana, Geneva, sans-serif;'> <thead><tr height='80'><th colspan='4' style='background-color:#f5f5f5; border-bottom:solid 1px #bdbdbd; font-family:Verdana, Geneva, sans-serif; color:#333; font-size:34px;'>Welcome</th></tr></thead> <tbody><tr align='center' height='50' style='font-family:Verdana, Geneva, sans-serif;background-color:#00a2d1;'><td colspan='4' align='center' style='background-color:#f5f5f5; border-top:dashed #00a2d1 2px; font-size:24px; '></tr><tr><td colspan='4' style='padding:15px;'><p style='font-size:16px;'>Hi {{name}},</p><p style='font-size:16px; font-family:Verdana, Geneva, sans-serif; text-justify: auto'> Thanks for signing up for our newsletter! You’re joining our amazing community.<br>As a token of our appreciation, use the code {{voucherCode}} to get {{voucherValue}} off your first purchase. </p><p style='font-size:16px;'>{{signature}}</p></td></tr><tr height='80'><td colspan='4' align='center' style='background-color:#f5f5f5; border-top:dashed #00a2d1 2px; font-size:24px; '><p style='font-size:10px; font-family:Verdana, Geneva, sans-serif; text-align:center'>Please do not reply to this email This is an automatic email. If you answer, it can not be read. This message has been sent to {{recipient}}</p><label>Find us on :<a href='#' target='_blank'><img style='vertical-align:middle' src='https://cdnjs.cloudflare.com/ajax/libs/webicons/2.0.0/webicons/webicon-facebook-m.png'/></a><a href='#' target='_blank'><img style='vertical-align:middle' src='https://cdnjs.cloudflare.com/ajax/libs/webicons/2.0.0/webicons/webicon-twitter-m.png'/></a><a href='#' target='_blank'><img style='vertical-align:middle' src='https://cdnjs.cloudflare.com/ajax/libs/webicons/2.0.0/webicons/webicon-googleplus-m.png'/></a><a href='#' target='_blank'><img style='vertical-align:middle' src='https://cdnjs.cloudflare.com/ajax/libs/webicons/2.0.0/webicons/webicon-rss-m.png'/></a></label></td></tr></tbody> </table> </td></tr></table>",
"TextPart": "Welcome {{name}},\r\nThanks for signing up for our newsletter! You’re joining our amazing community. As a token of our appreciation, use the code {{voucherCode}} to get {{voucherValue}} off your first purchase.."
}
}
This code contains the following properties:
- TemplateName — The name of the template. When you send the email, you refer to this name.
- SubjectPart — The subject line of the email.
- HtmlPart — The HTML body of the email. This property may contain replacement tags. These tags use the following format:
{{tagname}}. When you send the email, you can specify a value fortagnamefor each destination. (The HTML code of the template has been minified to a String) - TextPart — The text body of the email. Recipients whose email clients don’t display HTML email see this version of the email. This property may contain replacement tags.
At the command line, type the following command to create a new template using the CreateTemplate API operation:
aws ses create-template --cli-input-json file://NewsletterTemplate.json

Now, we can start coding 🚀.
Spring Boot App Setup
We will start by creating a simple Spring Boot project from start.spring.io, with the following dependencies: starter-web, Lombok, and starter-validation.
We use AWS SDK for Java 2.x with Amazon SES API v2. To get started, add the AWS SDK dependency to the pom.xml file. We use the 2.0.157 version.
<!-- https://mvnrepository.com/artifact/software.amazon.awssdk/sesv2 -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sesv2</artifactId>
<version>2.20.157</version>
</dependency>
The second important step is to add the AWS credentials to connect to Amazon SES in the properties file.
# AWS properties
aws:
access-key: <YOUR ACCESS KEY>
secret-key: <SECRET ACCESS KEY>
region: us-east-1
Configuring Beans SesV2Client
We need to create an AwsConfig class configuration to initialize SesV2Client which is the service client for accessing Amazon SES V2.
@RequiredArgsConstructor
@Configuration
public class AwsConfig {
private final AwsProperties awsProperties;
@Bean
public SesV2Client sesV2Client() {
return SesV2Client.builder()
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(awsProperties.getAccessKey(), awsProperties.getSecretKey())))
.region(Region.of(awsProperties.getRegion()))
.build();
}
}
Sending Email Template
Let’s define a service layer to use this bean to send email templates.
@RequiredArgsConstructor
@Slf4j
@Service
public class MailService {
private final SesV2Client sesV2Client;
@Async
public void sendEmailTemplate(EmailNotificationSender notificationSender) {
var templateData = modelDataSerializer(notificationSender.getTemplateData());
SendEmailRequest emailRequest = SendEmailRequest.builder()
.destination(d -> d.toAddresses(notificationSender.getMailTo()).bccAddresses(notificationSender.getMailBcc()).ccAddresses(notificationSender.getMailCc()))
.content(c -> c.template(t -> t.templateName(notificationSender.getProcessType().getTemplateName()).templateData(templateData)))
.fromEmailAddress(notificationSender.getFrom())
.build();
var response = sesV2Client.sendEmail(emailRequest);
log.info("email based on a template was sent. Response {}", response.toString());
}
/**
* @param templateData Map template data
* @return String json template data
*/
private String modelDataSerializer(Map<String, String> templateData) {
ObjectMapper mapperObj = new ObjectMapper();
try {
return mapperObj.writeValueAsString(templateData);
} catch (JsonProcessingException e) {
log.error("Error parsing templateData");
throw new BadRequestException("Template Date error");
}
}
}
The sendEmailTemplatemethod uses an EmailNotificationSenderthat will be sent by the controller API.
sendEmailRequest – Represents a request to send a single formatted email using Amazon SES.
Finally, we will define the Controller layer to pass the EmailNotificationSender object.
@RequiredArgsConstructor
@Validated
@RestController
public class SenderController {
private final SenderService senderService;
@PostMapping("/email")
public ResponseEntity<String> sendEmailNotification(@RequestBody @Valid EmailNotificationSender emailNotificationSender) {
senderService.sendEmail(emailNotificationSender);
return new ResponseEntity<>("email sent", HttpStatus.OK);
}
}
Test the REST API:
Run the Application.

The recipient has received the email with the email template.

Conclusion
Well done !!. In this post, We have seen how to send templated emails with Amazon SES.
The complete source code is available on GitHub.
You can reach out to me and follow me on Medium, Twitter, GitHub
Thanks for reading!