Menu
Amazon Simple Email Service
Developer Guide

Send an Email 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.

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 Email Sending in Amazon SES.

Prerequisites

Before you begin, perform the following tasks:

  • Verify your email address with Amazon SES—Before you can send an email with Amazon SES, you must verify that you own the sender's email address. If your account is still in the Amazon SES sandbox, you must also verify the recipient email address. The easiest way to verify email addresses is by using the Amazon SES console. For more information, see Verifying Email Addresses in Amazon SES.

  • Get your AWS credentials—You need an AWS access key ID and AWS secret access key to access Amazon SES using an SDK. You can find your credentials by using the Security Credentials page in the AWS Management Console. For more information about credentials, see Using Credentials With Amazon SES.

  • Install Eclipse—Eclipse is available at https://www.eclipse.org/downloads. The code in this tutorial was tested using Eclipse Neon.3 (version 4.6.3), running version 1.8 of the Java Runtime Environment.

  • Install the AWS Toolkit for Eclipse—Instructions for adding the AWS Toolkit for Eclipse to your Eclipse installation are available at https://aws.amazon.com/eclipse. The code in this tutorial was tested using version 2.3.1 of the AWS Toolkit for Eclipse.

  • Create a shared credentials file—For the sample code in this section to function properly, you must create a shared credentials file. For more information, see Create a Shared Credentials File.

Procedure

The following procedure shows how to send an email through Amazon SES using the AWS SDK for Java.

To send an email using the AWS SDK for Java

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

    1. Start Eclipse.

    2. On the File menu, choose New, and then choose Other. On the New window, expand the AWS folder, and then choose AWS Java Project.

    3. In the New AWS Java Project dialog box, do the following:

      1. For Project name, type a project name.

      2. Under AWS SDK for Java Samples, select Amazon Simple Email Service JavaMail Sample.

      3. Choose Finish.

  2. In Eclipse, in the Package Explorer pane, expand your project.

  3. Under your project, expand the src/main/java folder, expand the com.amazon.aws.samples folder, and then double-click AmazonSESSample.java.

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

    package com.amazonaws.samples; import java.io.IOException; 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; public class AmazonSESSample { // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. static final String FROM = "sender@example.com"; // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. static final String TO = "recipient@example.com"; // The configuration set to use for this email. If you do not want to use a // configuration set, comment the following variable and the // .withConfigurationSetName(CONFIGSET); argument below. static final String CONFIGSET = "ConfigSet"; // The subject line for the email. static final String SUBJECT = "Amazon SES test (AWS SDK for Java)"; // The HTML body for the email. static final String HTMLBODY = "<h1>Amazon SES test (AWS SDK for Java)</h1>" + "<p>This email was sent with <a href='https://aws.amazon.com/ses/'>" + "Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-java/'>" + "AWS SDK for Java</a>"; // The email body for recipients with non-HTML email clients. static final String TEXTBODY = "This email was sent through Amazon SES " + "using the AWS SDK for Java."; public static void main(String[] args) throws IOException { try { AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard() // Replace US_WEST_2 with the AWS Region you're using for // Amazon SES. .withRegion(Regions.US_WEST_2).build(); SendEmailRequest request = new SendEmailRequest() .withDestination( new Destination().withToAddresses(TO)) .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) // Comment or remove the next line if you are not using a // configuration set .withConfigurationSetName(CONFIGSET); client.sendEmail(request); System.out.println("Email sent!"); } catch (Exception ex) { System.out.println("The email was not sent. Error message: " + ex.getMessage()); } } }
  5. 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 Identities 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.

    • US_WEST_2—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.

  6. Save AmazonSESSample.java.

  7. To build the project, choose Project and then choose Build Project.

    Note

    If this option is disabled, automatic building may be enabled; if so, skip this step.

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

  9. Review the output of the console pane in Eclipse. If the email was successfully sent, the console displays "Email sent!" Otherwise, it displays an error message.

  10. If the email was successfully sent, sign in to the email client of the recipient address. You will find the message that you sent.

On this page: