1. Introduction

In this article, we’re going to build a java program that uses SES template to send personalized emails. We will create replacement tags under the template and apply personalized values while sending the email.

This article is part of the ‘AWS Simple Email Service‘ series here at CloudTechPro.

2. Configuring Email Addresses

2.1. Verifying Email Addresses

First of all, we need 2 verified email addresses. We use these addresses as a sender and receiver.

If you don’t have verified emails, check out AWS’s documentation for verifying email addresses in SES.

2.2. Replace Emails from Sample Program

To run the sample program, you also need to replace sample emails from your own verified emails. Let’s replace the following emails from the sample program.

String senderEmail = "satishpandey.soft@gmail.com";
Pair<String, String> contact = new Pair<String, String>("satish@cloudtechpro.com", "{ \"user\":\"Satish\" }");

3. Setting up Template

Now for our sample program, we need a template with a single replacement tag.

String emailText = Hello {{user}} - I am Satish from CloudTechPro.

In the above email template {{user}} is a replacement tag. This tag will be replaced by some actual value each time while sending email to a contact or list of contacts.

Let’s create a template using the above email text.

private static void createTemplate() {
    Template template = new Template();
    template.setSubjectPart("Test mail");
    CreateTemplateRequest createTemplateRequest = new CreateTemplateRequest();
    CreateTemplateResult result = ses.createTemplate(createTemplateRequest);

In the above code, we created a simple template with user tag. Now we can reuse this template by placing user tag value for every contact.

Let’s see how to delete the template after sending our test mail.

private static void deleteTemplate() {
    DeleteTemplateRequest deleteTemplateRequest = new DeleteTemplateRequest();
    DeleteTemplateResult result = ses.deleteTemplate(deleteTemplateRequest);

This will delete the template created during the sample run.

4. Sending Email

Before sending the email, we need to set template data to the email request. We can send JSON based key-value pairs as template data. We only have a single tag in our template. Let’s send Satish as a user.

"{ \"user\":\"Satish\" }"

After preparing template data its time to send the email.

private static void sendEmail() {
    Destination destination = new Destination();
    List<String> toAddresses = new ArrayList<String>();
    SendTemplatedEmailRequest templatedEmailRequest = new SendTemplatedEmailRequest();
    SendTemplatedEmailResult templatedEmailResult = ses.sendTemplatedEmail(templatedEmailRequest);

That’s it. You just sent an email using a template from AWS SES service.

5. Conclusion

In this article, we’ve seen how to send personalized email using SES template. We’ve created an email template and prepared template data while sending email using SES service.

As always, sample program can be found over on GitHub.

Sending Personalized Emails using SES Template
Tagged on:             

Satish Pandey

I am an expert Java Spring Angular developer with 10+ years of rich and varied experience in developing end-to-end Web Applications. I maintain this blog and publish articles in my free time to help the community. Email: satish@cloudtechpro.com

Leave a Reply

Notify of