[Java] Send an email using Amazon SES


Since I had the opportunity to use AWS's SES (Simple Email Service), I would like to summarize how to send an email with a Java program as a review. There are two methods, one is to use SMTP and the other is to use AWS SDK. For now, I will describe the SMTP method. I will add the AWS SDK at a later date.

About Amazon SES

--Amazon SES (Simple Email Service) is an email delivery service provided by Amazon. --Low introduction cost, small scale and inexpensive operation possible --You can send the first 62,000 emails of each month for free (SES Fee) --Email logs can be saved and analyzed by using other AWS services

Preparation (SES console)

Before creating the program, you need to do the following:

Source (From) address authentication, destination (To) address authentication

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


  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 you want to authenticate 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.

This completes the verification of your email address. You can check if you can send and receive emails by clicking the "Send a Test Email" button.

Create an SMTP user to 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 name of the SMTP user and click the "Create" button (the name can be the default value)
  5. Click Show User SMTP Security Credentials
  6. Copy the displayed credentials and save them in a safe place or download csv with "Download Credentials"

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

Operating environment

$ 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 Maven project in eclipse
  2. Add JavaMail dependency to pom.xml (search JavaMail in MvnRepository and add the latest version of jar)
  3. Create an email sending program




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 user name and password created in the SES console
    static final String SMTP_USERNAME = "smtp_username";
    static final String SMTP_PASSWORD = "smtp_password";

    //Specify the Config Set created in the SES console. Used when saving mail logs. Comment out because it is unnecessary this time
    // static final String CONFIGSET = "ConfigSet";

    //Amazon SES SMTP Endpoint(Us if the region is Oregon-west-2)
    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)";
    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 {
		//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");

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

    	//Compose an 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 it this time, so comment it out
        //msg.setHeader("X-SES-CONFIGURATION-SET", CONFIGSET);

        Transport transport = session.getTransport();

        //send e-mail
        try {

            //Connect to 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 {
            //End of connection

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

in conclusion

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

Recommended Posts

[Java] Send an email using Amazon SES
Send email using Amazon SES SMTP in Java
Send an email using JavaMail on AWS
I want to send an email in Java.
[Spring Boot] Send an email
[2020 version] How to send an email using Android Studio Javamail
I sent an email in Java
Formatting an enum using formatter-maven-plugin (Java)
Send an email from gmail with Ruby
Map without using an array in java
[Java] Creating an Excel file using Apache POI
Send push notifications using Notification Hubs in Java
I tried using an extended for statement in Java
Send an email with a PDF attachment via JavaMail
Sorting using java comparator
[Java] Install Amazon Corretto 8
[java] throw an exception
Scraping practice using Java ②
Scraping practice using Java ①
I want to build Java Applet without using an IDE
I want to manually send an authorization email with Devise