Aws simple mail service with java

AWS Simple Email Service (SES) is a highly scalable and cost-effective service that allows you to send and receive emails. Here's a step-by-step guide on how to use AWS SES with Java:

Prerequisites

  1. Create an AWS account and sign up for SES.
  2. Verify your email address or domain with SES.
  3. Install the AWS SDK for Java (version 1.11.635 or later).

Step 1: Set up your AWS credentials

Create a file named aws-credentials.properties in the root of your project with the following content:

aws.accessKeyId=YOUR_ACCESS_KEY_ID
aws.secretKey=YOUR_SECRET_ACCESS_KEY

Replace YOUR_ACCESS_KEY_ID and YOUR_SECRET_ACCESS_KEY with your actual AWS access key ID and secret access key.

Step 2: Create a Java class to send emails

Create a new Java class, e.g., EmailSender.java, with the following code:

import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.ses.model.SendEmailRequest;
import software.amazon.awssdk.services.ses.model.SendEmailResponse;

public class EmailSender {
    public static void main(String[] args) {
        // Create an SES client
        SesClient sesClient = SesClient.create();

        // Set the sender and recipient email addresses
        String senderEmail = "[email protected]";
        String recipientEmail = "[email protected]";

        // Set the email subject and body
        String subject = "Test email from AWS SES";
        String body = "This is a test email sent from AWS SES";

        // Create a SendEmailRequest object
        SendEmailRequest request = SendEmailRequest.builder()
               .destination(SendEmailRequest.Destination.builder()
                       .toAddresses(recipientEmail)
                       .build())
               .message(SendEmailRequest.Message.builder()
                       .body(SendEmailRequest.Body.builder()
                               .text(BuildableText.builder()
                                       .data(body)
                                       .build())
                               .build())
                       .subjectData(SendEmailRequest.SubjectData.builder()
                               .data(subject)
                               .build())
                       .build())
               .source(senderEmail)
               .build();

        // Send the email
        SendEmailResponse response = sesClient.sendEmail(request);

        // Check the response
        System.out.println(response.toString());
    }
}

Step 3: Run the Java class

Compile and run the EmailSender.java class. This will send an email from your sender email address to the recipient email address.

Tips and Variations