AWS: Simple Email Service (Sample Java Example)


AWS SES, a cloud-based email service provider that allows you to send and receive email using your own email addresses and domains. AWS SES is a cost-effective, flexible, and scalable solution for various email use cases, such as marketing, transactional, notification, and bulk emails. AWS SES also integrates seamlessly with other AWS products and services, such as SNS, S3, Lambda, KMS, CloudTrail, CloudWatch, and Kinesis Data Firehose. In this blog, you will find posts about how to set up, configure, use, and monitor AWS SES for your email needs.


Dependencies

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

Sample Code (JAVA)

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
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;
import java.util.List;

private String sender; //your sender email
private String emailRegion; //your email region
private String accessKey; //aws access key
private String secretKey; // aws secret key
private static final String CHAR_SET = "UTF-8";


public void sendSESMail(List<String> receiver, String textContent, String htmlContent, String subject) {

    AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard().withRegion(emailRegion)
            .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
            .build();
    SendEmailRequest request = new SendEmailRequest().withDestination(new Destination().withToAddresses(receiver))
            .withMessage(new Message()
                    .withBody(new Body().withHtml(new Content().withCharset(CHAR_SET).withData(htmlContent))
                            .withText(new Content().withCharset(CHAR_SET).withData(textContent)))
                    .withSubject(new Content().withCharset(CHAR_SET).withData(subject)))
            .withSource(sender);
    client.sendEmail(request);
}

Reference

  • https://aws.amazon.com/ses/

Leave a Reply