How to Send Email using Amazon SES in Java Spring Boot

  • Last updated Apr 25, 2024

In this example, you will learn how to send a plain-text or HTML-based email using Amazon SES in a Java Spring Boot application. You'll need to configure your Spring Boot project with the necessary dependencies and settings for sending email.

Follow these steps to achieve this:

  1. Setting up Amazon SES:
    • Login to the AWS Management Console and open the AWS SES console at https://console.aws.amazon.com/ses.
    • Create identity.
    • Verify your email address with Amazon SES.
    • Get your AWS credentials.
  2. Adding Dependency:
  3. Add Java SDK for Amazon SES dependency to your project from maven.

    For Maven:

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-ses</artifactId>
        <version>1.12.564</version>
    </dependency>

    For Gradle:

    implementation group: 'com.amazonaws', name: 'aws-java-sdk-ses', version: '1.12.564'
  4. Adding Configuration:
  5. Create a class named AwsConfig and annotate it with @Configuration annotation. Add a method that returns an instance of AmazonSimpleEmailService class and annotate it with @Bean annotation. The following code shows how to do so:

    package com.example.app.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import com.amazonaws.auth.AWSStaticCredentialsProvider;
    import com.amazonaws.auth.BasicAWSCredentials;
    import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
    import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
    
    @Configuration
    public class AwsConfig {
    
      public AWSStaticCredentialsProvider awsCredentials() {
        BasicAWSCredentials credentials =
            new BasicAWSCredentials("your-aws-access-key", "your-aws-access-secret");
        return new AWSStaticCredentialsProvider(credentials);
      }
    
      @Bean
      public AmazonSimpleEmailService getAmazonSimpleEmailService() {
        return AmazonSimpleEmailServiceClientBuilder.standard().withCredentials(awsCredentials())
            .withRegion("us-east-1").build();
      }
    }
  6. Creating a Method for Sending Email:
  7. The following code shows you how to send email using Amazon SES in Java:

    Note: HTML based email content only support inline style css.

    package com.example.app.aws.ses;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
    import com.amazonaws.services.simpleemail.model.Body;
    import com.amazonaws.services.simpleemail.model.Content;
    import com.amazonaws.services.simpleemail.model.Destination;
    import com.amazonaws.services.simpleemail.model.Message;
    import com.amazonaws.services.simpleemail.model.SendEmailRequest;
    
    @Component
    public class EmailService {
    
      @Autowired
      public AmazonSimpleEmailService amazonSimpleEmailService;
    
      public void sendEmail() {
    
        String emailContent =
            "<!DOCTYPE html>\n" + "<html lang=\"en\">\n" + "<head>\n" + "    <meta charset=\"utf-8\">\n"
                + "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
                + "    <title>Example HTML Email</title>\n" + "</head>\n"
                + "<body style=\"background: whitesmoke; padding: 30px; height: 100%\">\n"
                + "<h5 style=\"font-size: 18px; margin-bottom: 6px\">Dear example,</h5>\n"
                + "<p style=\"font-size: 16px; font-weight: 500\">Greetings from TutorialsBuddy</p>\n"
                + "<p>This is a simple html based email.</p>\n" + "</body>\n" + "</html>";
    
        String senderEmail = "sender@tutorialsbuddy.com";
        String receiverEmail = "receiver@example.com";
        String emailSubject = "Test Email";
    
        try {
          SendEmailRequest sendEmailRequest = new SendEmailRequest()
              .withDestination(new Destination().withToAddresses(receiverEmail))
              .withMessage(new Message()
                  .withBody(
                      new Body().withHtml(new Content().withCharset("UTF-8").withData(emailContent)))
                  .withSubject(new Content().withCharset("UTF-8").withData(emailSubject)))
              .withSource(senderEmail);
          amazonSimpleEmailService.sendEmail(sendEmailRequest);
    
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    
    }