Menu
Amazon Simple Email Service
Developer Guide (API Version 2010-12-01)

Send an Email Through Amazon SES Using the AWS SDK for Java

The following procedure shows you how to use Eclipse IDE for Java EE Developers and AWS Toolkit for Eclipse to create an AWS SDK project and modify the Java code to send an email through Amazon SES. It retrieves your AWS credentials from environment variables.

Before you begin this procedure, complete the setup tasks described in Before You Begin with Amazon SES and Send an Email Through Amazon SES Using an AWS SDK.

Important

In this getting started tutorial, you send an email to yourself so that you can check to see if you received it. For further experimentation or load testing, use the Amazon SES mailbox simulator. Emails that you send to the mailbox simulator do not count toward your sending quota or your bounce and complaint rates. For more information, see Testing Amazon SES Email Sending.

To send an email using the AWS SDK for Java

  1. Create an environment variable called AWS_ACCESS_KEY_ID and set it to your AWS access key ID. The procedure for setting environment variables depends on your operating system. Your AWS access key ID will look something like: AKIAIOSFODNN7EXAMPLE.

  2. Create an environment variable called AWS_SECRET_ACCESS_KEY and set it to your AWS secret access key. Your AWS secret access key will look something like: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY.

  3. Create an AWS Java Project in Eclipse by performing the following steps:

    1. Open Eclipse.

    2. In Eclipse, choose File, choose New, and then choose AWS Java Project. If you do not see AWS Java Project as an option, try selecting Other.

    3. In the Create an AWS Java Project dialog box, type a project name.

    4. Choose Finish.

  4. In Eclipse, in the Package Explorer window, expand your project.

  5. Under your project, right-click the src directory, choose New, and then choose Class.

  6. In the Java Class dialog box, in the Name field, type AmazonSESSample and then choose Finish.

  7. Replace the entire contents of AmazonSESSample.java with the following code:

    Copy
    import java.io.IOException; import com.amazonaws.services.simpleemail.*; import com.amazonaws.services.simpleemail.model.*; import com.amazonaws.regions.*; public class AmazonSESSample { static final String FROM = "SENDER@EXAMPLE.COM"; // Replace with your "From" address. This address must be verified. static final String TO = "RECIPIENT@EXAMPLE.COM"; // Replace with a "To" address. If your account is still in the // sandbox, this address must be verified. static final String BODY = "This email was sent through Amazon SES by using the AWS SDK for Java."; static final String SUBJECT = "Amazon SES test (AWS SDK for Java)"; public static void main(String[] args) throws IOException { // Construct an object to contain the recipient address. Destination destination = new Destination().withToAddresses(new String[]{TO}); // Create the subject and body of the message. Content subject = new Content().withData(SUBJECT); Content textBody = new Content().withData(BODY); Body body = new Body().withText(textBody); // Create a message with the specified subject and body. Message message = new Message().withSubject(subject).withBody(body); // Assemble the email. SendEmailRequest request = new SendEmailRequest().withSource(FROM).withDestination(destination).withMessage(message); try { System.out.println("Attempting to send an email through Amazon SES by using the AWS SDK for Java..."); // Instantiate an Amazon SES client, which will make the service call. The service call requires your AWS credentials. // Because we're not providing an argument when instantiating the client, the SDK will attempt to find your AWS credentials // using the default credential provider chain. The first place the chain looks for the credentials is in environment variables // AWS_ACCESS_KEY_ID and AWS_SECRET_KEY. // For more information, see http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(); // Choose the AWS region of the Amazon SES endpoint you want to connect to. Note that your sandbox // status, sending limits, and Amazon SES identity-related settings are specific to a given AWS // region, so be sure to select an AWS region in which you set up Amazon SES. Here, we are using // the US West (Oregon) region. Examples of other regions that Amazon SES supports are US_EAST_1 // and EU_WEST_1. For a complete list, see http://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html Region REGION = Region.getRegion(Regions.US_WEST_2); client.setRegion(REGION); // Send the email. client.sendEmail(request); System.out.println("Email sent!"); } catch (Exception ex) { System.out.println("The email was not sent."); System.out.println("Error message: " + ex.getMessage()); } } }
  8. In AmazonSESSample.java, replace the following with your own values:

    Important

    The email addresses are case-sensitive. Make sure that the addresses are exactly the same as the ones you verified.

    • SENDER@EXAMPLE.COM—Replace with your "From" email address. You must verify this address before you run this program. For more information, see Verifying Email Addresses and Domains in Amazon SES.

    • RECIPIENT@EXAMPLE.COM—Replace with your "To" email address. If your account is still in the sandbox, you must verify this address before you use it. For more information, see Moving Out of the Amazon SES Sandbox.

    • REGION—Set this to the AWS region of the Amazon SES endpoint you want to connect to. Note that your sandbox status, sending limits, and Amazon SES identity-related settings are specific to a given AWS region, so be sure to select an AWS region in which you set up Amazon SES. In this example, we are using the US West (Oregon) region. Examples of other regions that Amazon SES supports are US_EAST_1 and EU_WEST_1. For a complete list of AWS regions that Amazon SES supports, see Regions and Amazon SES.

  9. Save AmazonSESSample.java.

  10. To build the project, choose Project and then choose Build Project. (If this option is disabled, you may have automatic building enabled.)

  11. To start the program and send the email, choose Run and then choose Run again.

  12. Review the program's console output to verify that the sending was successful. (You should see "Email sent!")

  13. Log into the email client of the recipient address. You will find the message that you sent.