Menu
Amazon Simple Email Service
Developer Guide

Amazon SES Java Code Examples

This section contains the following sample code:

Sending Email Using the AWS SDK for Java

The following sample code is a complete solution for sending email through Amazon SES using the AWS SDK for Java. This sample code assumes that you have installed the AWS SDK for Java, and that you have created a shared credentials file. For more information about creating a shared credentials file, see Create a Shared Credentials File.

Copy
package com.amazonaws.samples; import java.io.IOException; import com.amazonaws.services.simpleemail.*; import com.amazonaws.services.simpleemail.model.*; import com.amazonaws.regions.*; public class AmazonSESSample { // Replace with your "From" address. This address must be verified. static final String FROM = "SENDER@EXAMPLE.COM"; // Replace with a "To" address. If your account is still in the // sandbox, this address must be verified. static final String TO = "RECIPIENT@EXAMPLE.COM"; static final String BODY = "This email was sent through Amazon SES 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 using the AWS SDK for Java."); // Instantiate an Amazon SES client, which will make the service // call. // // The service call requires your AWS credentials. This code // assumes that you are using the AWS Toolkit for Eclipse. // Further, it assumes that you specified your AWS // credentials in the AWS Preferences window in Eclipse. // // If you are using a different IDE, you can specify your // credentials as environment variables. The credential // provider chain looks for your AWS credentials in the // environment variables AWS_ACCESS_KEY_ID and // AWS_SECRET_ACCESS_KEY. For more information, see // http://docs.aws.amazon.com/ses/latest/DeveloperGuide/credentials.html // // When you instantiate the client, you can also specify the AWS // Region of the Amazon SES endpoint. 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. This example uses the // US West (Oregon) Region. For a complete list of AWS Regions, see // http://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard() .withRegion(Regions.US_WEST_2).build(); // 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()); } } }

Sending Email through the Amazon SES SMTP Interface Using Java

The following sample code is a complete solution for sending email through the Amazon SES SMTP interface using Java. In order to run this sample code, you must obtain SMTP credentials; for more information, see Obtaining Your Amazon SES SMTP Credentials. You must also download the JavaMail API.

Copy
import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; 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 the Amazon SES SMTP interface by using Java."; static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)"; // Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials. static final String SMTP_USERNAME = "YOUR_SMTP_USERNAME"; // Replace with your SMTP username. static final String SMTP_PASSWORD = "YOUR_SMTP_PASSWORD"; // Replace with your SMTP password. // Amazon SES SMTP host name. This example uses the US West (Oregon) Region. static final String HOST = "email-smtp.us-west-2.amazonaws.com"; // The port you will connect to on the Amazon SES SMTP endpoint. We are choosing port 25 because we will use // STARTTLS to encrypt the connection. static final int PORT = 25; public static void main(String[] args) throws Exception { // Create a Properties object to contain connection configuration information. Properties props = System.getProperties(); props.put("mail.transport.protocol", "smtps"); props.put("mail.smtp.port", PORT); // Set properties indicating that we want to use STARTTLS to encrypt the connection. // The SMTP session will begin on an unencrypted connection, and then the client // will issue a STARTTLS command to upgrade to an encrypted connection. props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.starttls.required", "true"); // Create a Session object to represent a mail session with the specified properties. Session session = Session.getDefaultInstance(props); // Create a message with the specified information. MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(FROM)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO)); msg.setSubject(SUBJECT); msg.setContent(BODY,"text/plain"); // Create a transport. Transport transport = session.getTransport(); // Send the message. try { System.out.println("Attempting to send an email through the Amazon SES SMTP interface..."); // Connect to Amazon SES using the SMTP username and password you specified above. transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD); // Send the email. transport.sendMessage(msg, msg.getAllRecipients()); System.out.println("Email sent!"); } catch (Exception ex) { System.out.println("The email was not sent."); System.out.println("Error message: " + ex.getMessage()); } finally { // Close and terminate the connection. transport.close(); } } }