HelloKoding

Practical coding guides

Sending Email Example with Java Mail API and Amazon SES SMTP Server

This tutorial will walk you through the steps of Sending Email With Amazon SES SMTP Server and Java Mail API.

Sending Email Example with Java Mail API and Amazon SES SMTP Server

What you’ll need

Stack

  • Java Mail API
  • AWS SES

Init project structure and dependencies

Project structure

├── src
│   └── main
│       └── java
│           └── com
│               └── hellokoding
│                   └── mail
│                       ├── Main.java
│                       └── SendingMailThroughAWSSESSMTPServer.java
└── pom.xml

Project dependencies

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hellokoding.mail</groupId>
    <artifactId>sendingmail-aws-sessmtpserver-javamailapi</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <exec.mainClass>com.hellokoding.mail.Main</exec.mainClass>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.0</version>
        </dependency>
    </dependencies>
</project>

Sending Mail with Java Mail API and AWS SES SMTP Server

SendingMailThroughAWSSESSMTPServer.java

package com.hellokoding.mail;


import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SendingMailThroughAWSSESSMTPServer {
    void sendMail(String smtpServerHost, String smtpServerPort,  String smtpUserName, String smtpUserPassword, String fromUserEmail, String fromUserFullName, String toEmail, String subject, String body) {
        try {
            Properties props = System.getProperties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.port", smtpServerPort);
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");

            Session session = Session.getDefaultInstance(props);

            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(fromUserEmail, fromUserFullName));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            msg.setSubject(subject);
            msg.setContent(body, "text/html");

            Transport transport = session.getTransport();
            transport.connect(smtpServerHost, smtpUserName, smtpUserPassword);
            transport.sendMessage(msg, msg.getAllRecipients());
        } catch (Exception ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
}

Run

Main.java

package com.hellokoding.mail;

public class Main {
    private static final String SMTP_SERVER_HOST = "email-smtp.us-west-2.amazonaws.com";
    private static final String SMTP_SERVER_PORT = "587";
    private static final String SUBJECT = "Sending mail with SES SMTP and Java Mail";
    private static final String BODY = "Hi,<br><br>This is a programmatic email.";

    public static void main(String[] args) {
        final String SMTP_USER_NAME = args[0];
        final String SMTP_USER_PASSWORD = args[1];
        final String FROM_USER_EMAIL = args[2];
        final String FROM_USER_FULLNAME = args[3];
        final String TO_USER_EMAIL = args[4];

        SendingMailThroughAWSSESSMTPServer sendingMailThroughAWSSESSMTPServer = new SendingMailThroughAWSSESSMTPServer();
        sendingMailThroughAWSSESSMTPServer.sendMail(SMTP_SERVER_HOST, SMTP_SERVER_PORT, SMTP_USER_NAME, SMTP_USER_PASSWORD, FROM_USER_EMAIL, FROM_USER_FULLNAME, TO_USER_EMAIL, SUBJECT, BODY);
    }
}

Replace below macros with your respective values and run the command

mvn clean package exec:java -Dexec.args="{SMTP_USER_NAME} {SMTP_USER_PASSWORD} {FROM_USER_EMAIL} {FROM_USER_FULLNAME} {TO_USER_EMAIL}"
Sending Email Example with Java Mail API and AWS SES SMTP Server

Source code

https://github.com/hellokoding/hellokoding-courses/tree/master/java-examples/javamail-awsses

Follow HelloKoding