Mastering Email Delivery: Using AWS SES with SMTP in Java
Introduction:
AWS Simple Email Service (SES) is a scalable email service designed to help businesses send marketing, notification, and transactional emails. It provides high deliverability rates and easy integration with applications using standard SMTP protocols.
1. Prerequisites
Before you start, ensure you have the following:
Before you start, ensure you have the following:
- An AWS account. If you don’t have one, you can create it https://aws.amazon.com/. A verified email address or d omain in AWS SES.
- Basic knowledge of email clients or SMTP libraries in your preferred programming language.
2. Setting Up AWS SES
Step 1: Verify Your Email Address or Domain
Before you can send emails, you need to verify your email address or domain with AWS SES.
Verify an Email Address:
Step 1: Verify Your Email Address or Domain
Before you can send emails, you need to verify your email address or domain with AWS SES.
Verify an Email Address:
- Go to the AWS SES console.
- In the left navigation pane, click Email Addresses.
- Click Verify a New Email Address.
- Enter your email address and click Verify This Email Address.
- Check your email inbox for a verification email from AWS SES and click the verification link.
Verify a Domain:
- In the AWS SES console, click Domains in the left navigation pane.
- Click Verify a New Domain.
- Enter your domain name and click Verify This Domain.
- Follow the instructions to add DNS records to your domain’s DNS configuration.
- Once the DNS records are verified, your domain will be verified in AWS SES.
Step 2: Request Production Access
By default, AWS SES operates in a sandbox environment with certain sending limits. To lift these limits and send emails to unverified addresses, request production access.
By default, AWS SES operates in a sandbox environment with certain sending limits. To lift these limits and send emails to unverified addresses, request production access.
- Go to the AWS SES console.
- In the left navigation pane, click Account Dashboard.
- Click Request a Sending Limit Increase.
- Fill out the request form and submit it.
3. Configuring SMTP Settings in AWS SES
Step 1: Create SMTP Credentials
Step 1: Create SMTP Credentials
- In the AWS SES console, click SMTP Settings in the left navigation pane.
- Click Create My SMTP Credentials.
- Enter a username (e.g., ses-smtp-user) and click Create.
- Download the SMTP credentials (SMTP username and password).
4. Sending Emails Using SMTP in Java
Now that you have your SMTP credentials, you can use them to send emails from your Java application. We’ll use JavaMail for this purpose.
Step 1: Add JavaMail Dependency
Add the following dependencies to your pom.xml file:<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
Now that you have your SMTP credentials, you can use them to send emails from your Java application. We’ll use JavaMail for this purpose.
Step 1: Add JavaMail Dependency
Add the following dependencies to your pom.xml file:<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
Step 2: Write Java Code to Send an Email
Use the following Java code to send an email using AWS SES via SMTP:import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
Use the following Java code to send an email using AWS SES via SMTP:import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailSender {
public static void main(String[] args) {
// SMTP credentials
final String SMTP_USERNAME = “YOUR_SMTP_USERNAME”;
final String SMTP_PASSWORD = “YOUR_SMTP_PASSWORD”;
public static void main(String[] args) {
// SMTP credentials
final String SMTP_USERNAME = “YOUR_SMTP_USERNAME”;
final String SMTP_PASSWORD = “YOUR_SMTP_PASSWORD”;
final String HOST = “email-smtp.us-east-1.amazonaws.com”;
final int PORT = 587;
final int PORT = 587;
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”);
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”);
Session session = Session.getDefaultInstance(props);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(“sender@example.com”, “Sender Name”));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(“recipient@example.com“));
msg.setSubject(“Test Email from AWS SES”);
msg.setContent(“This is a test email sent using AWS SES and SMTP.”, “text/html”);
msg.setFrom(new InternetAddress(“sender@example.com”, “Sender Name”));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(“recipient@example.com“));
msg.setSubject(“Test Email from AWS SES”);
msg.setContent(“This is a test email sent using AWS SES and SMTP.”, “text/html”);
Transport transport = session.getTransport();
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println(“Email sent successfully!”);
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Conclusion
AWS SES with SMTP is a powerful combination for sending emails seamlessly and cost-effectively. By following the steps outlined in this guide, you can set up AWS SES and integrate it into your Java applications to manage and send emails efficiently.
AWS SES with SMTP is a powerful combination for sending emails seamlessly and cost-effectively. By following the steps outlined in this guide, you can set up AWS SES and integrate it into your Java applications to manage and send emails efficiently.