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.
--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
Before creating the program, you need to do the following:
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.
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.
You can see the SMTP user you created in IAM Console> Access Management> Users.
$ 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)
pom.xml
<!--Dependencies-->
<dependencies>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
SesSmtpSample.java
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)";
//Text
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 {
//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));
msg.setSubject(SUBJECT);
msg.setContent(BODY,"text/html");
//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 {
System.out.println("Sending...");
//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
transport.close();
}
}
}
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).
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