Send Email Message Using AWS SES Java SDK

In this Java programming tutorial I would like to share with you a sample Java code that uses Amazon AWS SES(Simple Email Service) Java SDK to send an email message. I used this code implement Email Verification feature for my mobile app. The code below would send an email message containing an email verification link and when user clicks on that link a database record is updated with a boolean value indicating that user has successfully verified their email address.

For the below code to work, you will need to:

  • Sign up for Amazon AWS,
  • Verify your email or domain name with AWS SES(Simple Email Service),
  • Move your account out of AWS SES Sandbox. Other wise an email message can be sent only to a verified email address.

If you are not sure how to use AWS SES to verify your email address, then there is a very good documentation online and if you still need help, I have recorded a set of videos that will teach you step by step how to:

  • Verify email address with AWS SES,
  • Set up a very simple process to handle Bounces and Complains,
  • Submit a request to move your account out of AWS SES Sandbox.

I have made these videos part of my video course: REST API with Java JAX-RS. Build and Deploy to Amazon Cloud.

Download AWS SES Java SDK

I used Maven to download and add Amazon AWS SES Java SDK to my project. You can use the below xml to add SES Java SDK dependency to your project’s pom.xml file if you are using Maven, other wise you can download AWS SES Java SDK Jar here:

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-ses -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-ses</artifactId>
    <version>1.11.232</version>
</dependency>

Send Email Message Using AWS SES Java SDK

For the code example below to actually send email message you will need to replace the value of the FROM with a value of a verified email address.

import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
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;

/**
 *
 * @author sergeykargopolov
 */
public class AmazonSES {
      // This address must be verified with Amazon SES.
  final String FROM = "<SENDER EMAIL ADDRESS HERE>";
 
  // The subject line for the email.
  final String SUBJECT = "One last step to complete your registration with PhotoApp";
  
  // The HTML body for the email.
  final String HTMLBODY = "<h1>Please verify your email address</h1>"
      + "<p>Thank you for registering with our mobile app. To complete registration process and be able to log in,"
      + " click on the following link: " 
      + "<a href='http://localhost:8080/MOBILE-APP-WS-DEV/verify-email.jsp?token=$tokenValue'>"
      + "Final step to complete your registration"
      + "</a><br/><br/>"
      + "Thank you! And we are waiting for you inside!";

  // The email body for recipients with non-HTML email clients.
  final String TEXTBODY = "Please verify your email address. "
      + "Thank you for registering with our mobile app. To complete registration process and be able to log in,"
      + " open then the following URL in your browser window: " 
      + " http://localhost:8080/MOBILE-APP-WS-DEV/verify-email.jsp?token=$tokenValue"
      + " Thank you! And we are waiting for you inside!";
  
  public void verifyEmail()
  {
      try {
      AmazonSimpleEmailService client = 
          AmazonSimpleEmailServiceClientBuilder.standard()
            .withRegion(Regions.US_EAST_1).build();
    
      SendEmailRequest request = new SendEmailRequest()
          .withDestination(
              new Destination().withToAddresses( FROM ) )
          .withMessage(new Message()
              .withBody(new Body()
                  .withHtml(new Content()
                      .withCharset("UTF-8").withData(HTMLBODY))
                  .withText(new Content()
                      .withCharset("UTF-8").withData(TEXTBODY)))
              .withSubject(new Content()
                  .withCharset("UTF-8").withData(SUBJECT)))
          .withSource(FROM);

      client.sendEmail(request);
      
      System.out.println("Email sent!");
      
    } catch (Exception ex) {
        ex.printStackTrace();
    }
  }
  
  
 public static void main(String[] args)
  {
      new AmazonSES().verifyEmail();
  }
  
}

I hope this code example is helpful. If you are interested in learning more about Amazon AWS, check the books and video courses below.

Happy learning! 🙂

Learning Amazon AWS – Books

Learning Amazon AWS – Video Courses