AWS: Send Simple Email Service

This entry is part 5 of 5 in the series AWS & Java

If you want to send an email using AWS’ Simple Mail then you need to do the following. This is a very basic example.

Import the following:

  1. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  2. import com.amazonaws.auth.BasicAWSCredentials;
  3. import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
  4. import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
  5. import com.amazonaws.services.simpleemail.model.Body;
  6. import com.amazonaws.services.simpleemail.model.Content;
  7. import com.amazonaws.services.simpleemail.model.Destination;
  8. import com.amazonaws.services.simpleemail.model.Message;
  9. import com.amazonaws.services.simpleemail.model.SendEmailRequest;

Setup Connection to AWS Simple Email Service

  1. final AmazonSimpleEmailService simpleEmailService = AmazonSimpleEmailServiceClientBuilder.standard().withRegion(myRegion)
  2. .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKeyId, secretKey)))
  3. .build();

Setup Email:

  1. final SendEmailRequest request = new SendEmailRequest().withDestination(new Destination().withToAddresses(TO)).withSource(FROM)
  2. .withMessage(new Message().withSubject(new Content().withCharset("UTF-8").withData(SUBJECT))
  3. .withBody(new Body().withText(new Content().withCharset("UTF-8").withData(BODY))));

Send Email:

  1. simpleEmailService.sendEmail(request);