[Java] [Java] Sending email using Amazon SES

3 minute read


Since I had the opportunity to use SES (Simple Email Service) of AWS, I would like to summarize the method of sending email by Java program, also as a review. There are a method of using SMTP and a method of using AWS SDK, but for the moment, I will describe the method of SMTP. We will add AWS SDK later.

About Amazon SES

  • Amazon SES (Simple Email Service) is an email delivery service provided by Amazon.
  • Low installation cost, small scale and cheap operation possible
  • Send the first 62,000 emails every month free of charge (SES fee)
  • Mail logs can be saved and analyzed using other AWS services

Preparation (SES console)

Before creating a program, you need to:

Sender (From) address authentication, Destination (To) address authentication

The account is created as a new user in the test environment called sandbox, so you can only send and receive emails with the email address you have verified. To send emails to unverified email addresses, increase the number of emails you can send per day, and send emails fast, you need to move your account out of the sandbox.


  1. Log in to AWS and open the SES console
  2. Click “Email Addresses”
  3. Click the “Verify a New Email Address” button
  4. Enter the email address to be verified and click the “Verify This Email Address” button
  5. Click the link in the received email
  6. When you open the screen in step 2, the verification status of the specified email address is verified.

Your email address is now verified. You can click the “Send a Test Email” button to see if you can send and receive email.

Create an SMTP user and get credentials (username and password)


  1. Log in to AWS and open the SES console
  2. Click “SMTP Settings”
  3. Click the “Create My SMTP Credentials” button
  4. Enter the SMTP user name and click the “Create” button (the name can be the default value)
  5. Click View user’s SMTP security credentials
  6. Copy the displayed authentication information and save it in a safe place, or download csv with “Download authentication information”

You can see the SMTP user created in IAM Console> Access Management> Users.

Operating environment

  • eclipse Version: 2020-06 (4.16.0)
  • java version
$ java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

Java program

  1. Create a Maven project in eclipse
  2. Add dependency of JavaMail to pom.xml (Search JavaMail with MvnRepositoryandaddlatestversionjar)
  3. Creating an email sending program


  <!-- Dependencies -->


public class SesSmtpSample {

// Sender address and sender name (email address authenticated by SES console)
    static final String FROM = "[email protected]";
    static final String FROMNAME = "Sender Name";

    // Destination address (email address authenticated by SES console)
    static final String TO = "[email protected]";

    // SMTP username and password created in SES console
    static final String SMTP_USERNAME = "smtp_username";
    static final String SMTP_PASSWORD = "smtp_password";

    // Specify the Config Set created on the SES console. It is used when saving the mail log. This time it is unnecessary so comment out
    // static final String CONFIGSET = "ConfigSet";

    // Amazon SES SMTP endpoint (us-west-2 if region is Oregon)
    static final String HOST = "email-smtp.us-west-2.amazonaws.com";

    // Amazon SES SMTP endpoint port number.
    static final int PORT = 587;

    // Email subject
    static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)";
    // Text
    static final String BODY = String.join(
    "<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 {
It's a sequel.
// define SMTP server
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");

    // establish a mail session
    Session session = Session.getDefaultInstance(props);

    // compose email
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM,FROMNAME));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));

        // Set the Configuration Set. I will not use this time so comment out
        //msg.setHeader("X-SES-CONFIGURATION-SET", CONFIGSET);

        Transport transport = session.getTransport();

        // send e-mail
        try {

            // connect to the SMTP server
            transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);

            // send e-mail
            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 {
            // connection finished

SMTP endpoints vary by region. You can find it in AWS References (https://docs.aws.amazon.com/general/latest/gr/rande.html#ses_region).

in conclusion

Since there is a Japanese document for Amazon SES, basically I think you can send an email without problems if you follow the document.