Menu
Amazon Simple Email Service
Developer Guide

Send an Email Using SMTP with Java

This example uses the Eclipse IDE and the JavaMail API to send email through Amazon SES using the SMTP interface.

Before you perform the following procedure, complete the setup tasks described in Before You Begin with Amazon SES and Send an Email Through Amazon SES Using SMTP.

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.

To send an email using the Amazon SES SMTP interface with Java

  1. In a web browser, go to the JavaMail Github page. Under Downloads, choose javax.mail.jar to download the latest version of JavaMail.

    Important

    This tutorial requires JavaMail version 1.5 or later. These procedures were tested using JavaMail version 1.6.1.

  2. Create a project in Eclipse by performing the following steps:

    1. Start Eclipse.

    2. In Eclipse, choose File, choose New, and then choose Java Project.

    3. In the Create a Java Project dialog box, type a project name and then choose Next.

    4. In the Java Settings dialog box, choose the Libraries tab.

    5. Choose Add External JARs.

    6. Browse to the folder in which you downloaded JavaMail. Choose the file javax.mail.jar, and then choose Open.

    7. In the Java Settings dialog box, choose Finish.

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

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

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

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

    import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class AmazonSESSample { // Replace sender@example.com with your "From" address. // This address must be verified. static final String FROM = "sender@example.com"; static final String FROMNAME = "Sender Name"; // 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"; // Replace smtp_username with your Amazon SES SMTP user name. static final String SMTP_USERNAME = "smtp_username"; // Replace smtp_password with your Amazon SES SMTP password. static final String SMTP_PASSWORD = "smtp_password"; // The name of the Configuration Set to use for this message. // If you comment out or remove this variable, you will also need to // comment out or remove the header below. static final String CONFIGSET = "ConfigSet"; // Amazon SES SMTP host name. This example uses the US West (Oregon) region. // See http://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html#region-endpoints // for more information. static final String HOST = "email-smtp.us-west-2.amazonaws.com"; // The port you will connect to on the Amazon SES SMTP endpoint. static final int PORT = 587; static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)"; static final String BODY = String.join( System.getProperty("line.separator"), "<h1>Amazon SES SMTP Email Test</h1>", "<p>This email was sent with Amazon SES using the ", "<a href='https://github.com/javaee/javamail'>Javamail Package</a>", " for <a href='https://www.java.com'>Java</a>." ); 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", "smtp"); props.put("mail.smtp.port", PORT); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "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,FROMNAME)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO)); msg.setSubject(SUBJECT); msg.setContent(BODY,"text/html"); // Add a configuration set header. Comment or delete the // next line if you are not using a configuration set msg.setHeader("X-SES-CONFIGURATION-SET", CONFIGSET); // Create a transport. Transport transport = session.getTransport(); // Send the message. try { System.out.println("Sending..."); // 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(); } } }
  7. In AmazonSESSample.java, replace the following email addresses 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.

  8. In AmazonSESSample.java, replace the following SMTP credentials with the values that you obtained in Obtaining Your Amazon SES SMTP Credentials:

    Important

    Your SMTP credentials are different from your AWS credentials. For more information about credentials, see Using Credentials With Amazon SES.

    • YOUR_SMTP_USERNAME—Replace with your SMTP username credential. Note that your SMTP username credential is a 20-character string of letters and numbers, not an intelligible name.

    • YOUR_SMTP_PASSWORD—Replace with your SMTP password.

  9. (Optional) If you want to use an Amazon SES SMTP endpoint in a Region other than US West (Oregon), change the value of the variable HOST to the endpoint you want to use. For a list of Amazon SES endpoints, see Regions and Amazon SES.

  10. Save AmazonSESSample.java.

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

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

  13. Review the output. If the email was successfully sent, the console displays "Email sent!" Otherwise, it displays an error message.

  14. Sign into the email client of the recipient address. You will find the message that you sent.