Sending Personalized Emails using SES Template

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.setTemplateName(templateName);
    template.setSubjectPart("Test mail");
    template.setTextPart(emailText);
    CreateTemplateRequest createTemplateRequest = new CreateTemplateRequest();
    createTemplateRequest.setTemplate(template);
    CreateTemplateResult result = ses.createTemplate(createTemplateRequest);
    System.out.println(result.getSdkResponseMetadata());
}

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();
    deleteTemplateRequest.setTemplateName(templateName);
    DeleteTemplateResult result = ses.deleteTemplate(deleteTemplateRequest);
    System.out.println(result.getSdkResponseMetadata());
}

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>();
    toAddresses.add(contact.getKey());
    destination.setToAddresses(toAddresses);
    SendTemplatedEmailRequest templatedEmailRequest = new SendTemplatedEmailRequest();
    templatedEmailRequest.withDestination(destination);
    templatedEmailRequest.withTemplate(templateName);
    templatedEmailRequest.withTemplateData(contact.getValue());
    templatedEmailRequest.withSource(senderEmail);
    SendTemplatedEmailResult templatedEmailResult = ses.sendTemplatedEmail(templatedEmailRequest);
    System.out.println(templatedEmailResult.getMessageId());
}

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.

Leave a Reply

Your email address will not be published. Required fields are marked *