Introduction
AWS SES (Easy E-mail Service) is a simple-to-setup e mail sending and receiving service. It’s often tough, finicky and tedious to handle an on-premise e mail system, so outsourcing the method is a well-liked selection.
We will use Amazon SES to ship transactional emails, advertising and marketing emails, or another type of notification emails to our purchasers. It’s an reasonably priced resolution for companies of all sizes who use emails to attach with their clients since it’s constructed on the reliable and scalable infrastructure of Amazon Net Providers (AWS).
For sending bulk emails, any software might work together with Amazon SES. We solely pay for the emails we really ship, whether or not they’re transactional emails or advertising and marketing emails. Moreover, a variety of configurations, together with devoted, shared, or owned IP addresses, are supported by Amazon SES. Companies could make each e mail depend with the usage of deliverability dashboards and stories on sender data.
On this information we’re going to configure an AWS SES occasion in a area after which combine with Spring Cloud for AWS which is a sub-project of Spring Cloud. Lastly, we’ll check out completely different situations in which you will wish to ship emails from our software.
Observe: You’ll find all of the supply code used within the information on Github.
Lifecycle of Sending an E-mail utilizing AWS SES
Let’s take a look at how the lifecycle of an e mail despatched by an software reaches to vacation spot mailbox:
-
An software, in our case, the Spring Cloud code requests AWS SES to ship an e mail to a number of recipients.
-
SES initially verifies the request, and whether it is accepted, produces an e mail message with the request’s specs. This e mail message has a header, physique, and envelope and complies with RFC 5322‘s Web Message Format definition.
-
SES then transmits the message to the recipient’s receiver over the Web. As quickly because the message is handed off to SES, it’s typically transmitted immediately, with the preliminary supply try usually going down in a matter of milliseconds.
-
There are a number of outcomes at the moment. For example:
- Profitable Supply: The Web service supplier (ISP) accepts the e-mail and sends it to the meant recipient.
- Onerous Bounce: As a result of the recipient’s handle is invalid, the ISP rejects the e-mail. The ISP sends the onerous bounce notification again to Amazon SES, which notifies the sender by e mail or by publishing it to an Amazon Easy Notification Service (Amazon SNS) matter set as much as obtain this notification.
- Tender Bounce: Attributable to situations just like the receiver’s inbox being full, the area not current, or any passing circumstance just like the ISP being too busy to course of the request, the ISP may be unable to ship the e-mail to the recipient. The ISP then retries the e-mail as much as a sure variety of occasions and sends SES a smooth bounce message. If SES is unable to ship the e-mail throughout the specified timeframe, it both publishes the occasion to an SNS matter or sends a tough bounce message by way of e mail.
- Grievance: The e-mail is assessed as spam by the receiver of their e mail program. A criticism notification is transmitted to Amazon SES, which then relays it to the sender if Amazon SES and the ISP have a suggestions loop established up.
- Auto Response: The recipient ISP notifies Amazon SES of an automatic response from the receiver, similar to an out-of-office discover, and Amazon SES passes the notification to the sender.
When the supply is unsuccessful, Amazon SES returns an error to the sender and deletes the e-mail.
Setting Up Amazon SES
In contrast to another AWS Providers, there’s virtually no must create an SES occasion as all new AWS accounts are positioned within the AWS SES sandbox by default. Every AWS account has sandbox entry for AWS SES within the out there areas by default.
When utilizing sandbox mode, we will solely ship emails to verified identities. A site or e mail handle that we use to ship an e mail is a verified id. We should assemble and validate every id we intend to make use of as a From
, To
, Supply
, Sender
, or Return-Path
handle earlier than we will ship an e mail utilizing SES in sandbox mode. By utilizing Amazon SES to confirm the id, we will show our possession and cease unlawful use.
To keep away from fraud and protect the status of an IP handle, AWS SES contains e mail sending limits. These limitations specify the utmost variety of emails per second and the every day e mail restrict for every person. By getting in contact with the AWS Assist Middle, we might set up such quotas by area.
Let’s confirm identities. Login to AWS Console and seek for “Amazon Easy E-mail Service”:
Then click on on “Create Id” so as to add an e mail or a site for verification. In our case, we’re going to add an e mail for verification.
As soon as, the id is created, we will confirm the main points.
The id that we created goes into “Verification Pending” stage. At this stage, the person must test the verification mail from AWS and comply with the directions to get the e-mail verified.
Subsequent, we have to fetch “access-key” and “secret-key” for the authentication and authorization of our software with SES. To be able to generate that, we have to create a Consumer Group and add a Consumer to that group. Once we create that Consumer, AWS generates an access-key and secret-key. So let’s redirect to “IAM” in AWS Console and create Consumer Group.
Then we have to add “AdministratorAccess” permission to that group for SES.
Lastly, we are going to add a Consumer to the above group.
Subsequent, we have to choose the group for permissions.
Lastly, copy the access-key and secret-key displayed on the display for additional utilization.
Sending Emails utilizing Spring Cloud Undertaking
Undertaking Setup
Let’s spin up a Spring Cloud undertaking and run by the use-cases to combine with SES. The best method to begin with a skeleton undertaking is by way of Spring Initializr:
We have now added Spring Net for REST MVC, Apache Freemarker to generate HTML-based e mail templates, Java Mail Sender to ship an e mail and Lombok (non-compulsory boilerplate-reducing library) dependencies. Moreover, we have to add related dependencies for Spring Cloud AWS and SES. For Spring Cloud AWS, we are going to add a separate Spring Cloud AWS BOM in our pom.xml
file utilizing this <dependencyManagement>
block:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-dependencies</artifactId>
<model>2.3.0</model>
<kind>pom</kind>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Lastly, so as to add the help for SES, we have to embrace the module dependency which is obtainable as a starter module spring-cloud-starter-aws-ses
:
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-starter-aws-ses</artifactId>
</dependency>
spring-cloud-starter-aws-ses
contains the transitive dependencies for spring-cloud-starter-aws
, and spring-cloud-aws-ses
. The spring-cloud-aws-ses
module for SES comprises two lessons: SimpleEmailServiceMailSender
and SimpleEmailServiceJavaMailSender
.
- The
SimpleEmailServiceMailSender
class makes use of Amazon Easy E-mail Service to ship emails. The Java Mail API just isn’t a requirement for this implementation. It could be used to ship easy mail messages devoid of attachments. - The
SimpleEmailServiceJavaMailSender
class permits the sending of emails that comprise attachments and different mime components.
So this covers all our primary necessities!
Configuring Beans
As mentioned above, we have to outline two kinds of beans: SimpleEmailServiceMailSender
and SimpleEmailServiceJavaMailSender
. We will merely move the access-key and secret-key as credentials and configure a MailSender
bean which we are going to use to ship emails:
@Configuration
public class SesConfig {
@Worth("${cloud.aws.credentials.access-key}")
personal String accessKey;
@Worth("${cloud.aws.credentials.secret-key}")
personal String secretKey;
@Worth("${cloud.aws.area.static}")
personal String area;
@Bean
public AmazonSimpleEmailService amazonSimpleEmailService() {
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
return AmazonSimpleEmailServiceClientBuilder.commonplace()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(area)
.construct();
}
@Bean
public MailSender mailSender(AmazonSimpleEmailService amazonSimpleEmailService) {
return new SimpleEmailServiceMailSender(amazonSimpleEmailService);
}
@Bean
public JavaMailSender javaMailSender(AmazonSimpleEmailService amazonSimpleEmailService) {
return new SimpleEmailServiceJavaMailSender(amazonSimpleEmailService);
}
}
To be able to ship emails with attachments we have to configure the SimpleEmailServiceJavaMailSender
which is an implementation of the JavaMailSender
interface from Spring’s mail abstraction.
Take a look at our hands-on, sensible information to studying Git, with best-practices, industry-accepted requirements, and included cheat sheet. Cease Googling Git instructions and really be taught it!
We will even outline the properties to retrieve the knowledge from software.yml
:
cloud:
aws:
area:
static: eu-central-1
auto: false
stack:
auto: false
credentials:
access-key: ********
secret-key: **************************
Sending Easy E-mail
We will ship easy emails utilizing the SimpleEmailServiceMailSender
bean that we configured above. Let’s outline a service layer to make use of this bean:
@Service
public class EmailService {
@Autowired
personal MailSender mailSender;
public void sendMessage(SimpleMailMessage simpleMailMessage) {
this.mailSender.ship(simpleMailMessage);
}
}
We’re calling the ship()
technique within the MailSender
bean to ship our e mail. We additionally must move the SimpleMailMessage
that might comprise attributes like from
, to
, the textual content and topic for our e mail. So, let’s outline a Controller
class to name the above service utilizing a REST API:
@RestController
public class EmailController {
@Autowired
personal EmailService emailService;
@PostMapping("/sendEmail")
public String sendMessage(@RequestBody EmailDetails emailDetails) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(emailDetails.getFromEmail());
simpleMailMessage.setTo(emailDetails.getToEmail());
simpleMailMessage.setSubject(emailDetails.getSubject());
simpleMailMessage.setText(emailDetails.getBody());
emailService.sendMessage(simpleMailMessage);
return "E-mail despatched efficiently";
}
}
Now, if we run the applying and execute the next curl it’ll ship an e mail to the verified e mail handle:
curl -i -X POST
-H "Content material-Sort:software/json"
-d
'{
"fromEmail": "[email protected]",
"toEmail": "[email protected]",
"topic": "check e mail",
"physique": "Hello, This can be a check e mail."
}'
'http://localhost:8080/sendEmail'
Subsequent, we will login to the recipient’s mail handle and confirm if the recipient has acquired the e-mail.
Sending Easy E-mail with Attachment
We’ll outline a service layer to move the attachment as mime and set the opposite e mail attributes like from
, to
, textual content and topic:
@Service
public class EmailService {
@Autowired
personal JavaMailSender javaMailSender;
public void sendMessageWithAttachment(SimpleMailMessage simpleMailMessage) {
strive {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(
message,
MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
StandardCharsets.UTF_8.title());
helper.addAttachment("brand.png", new ClassPathResource("brand.png"));
helper.setTo(Objects.requireNonNull(simpleMailMessage.getTo()));
helper.setText(Objects.requireNonNull(simpleMailMessage.getText()));
helper.setSubject(Objects.requireNonNull(simpleMailMessage.getSubject()));
helper.setFrom(Objects.requireNonNull(simpleMailMessage.getFrom()));
javaMailSender.ship(message);
} catch (MessagingException e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
Right here we’re utilizing MimeMessageHelper
to create an e mail with an attachment. Lastly, we are going to outline Controller
layer to move the SimpleMailMessage
attributes:
@RestController
public class EmailController {
@Autowired
personal EmailService emailService;
@PostMapping("/sendEmailWithAttachment")
public String sendMessageWithAttachment(@RequestBody EmailDetails emailDetails) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(emailDetails.getFromEmail());
simpleMailMessage.setTo(emailDetails.getToEmail());
simpleMailMessage.setSubject(emailDetails.getSubject());
simpleMailMessage.setText(emailDetails.getBody());
emailService.sendMessageWithAttachment(simpleMailMessage);
return "E-mail despatched efficiently";
}
}
Now, if we run the applying and execute the next curl it’ll ship an e mail to the verified e mail handle:
curl -i -X POST
-H "Content material-Sort:software/json"
-d
'{
"fromEmail": "[email protected]",
"toEmail": "[email protected]",
"topic": "check e mail",
"physique": "Hello, This can be a check e mail with attachment."
}'
'http://localhost:8080/sendEmailWithAttachment'
Subsequent, we will login to the recipient’s mail handle and confirm if the recipient has acquired the e-mail.
Sending Template E-mail with Attachment
The earlier use-cases that we had seen is sweet for growth or check situations however in manufacturing, we typically use an e mail template with variables that might get replaced utilizing an API’s responses. We had earlier added the dependency for Apache Freemarker. We’ll use it to outline a template and cargo it to course of!
For this, let’s first outline a easy template, title it as email-template.ftl
and place it in templates
folder beneath sources
:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content material-Sort" content material="textual content/html; charset=utf-8" />
<title>StackAbuse E-mail</title>
</head>
<physique>
<desk width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="heart" valign="high" bgcolor="#838383"
type="background-color: #838383;"><br> <br>
<desk width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="heart" valign="high" bgcolor="#d3be6c"
type="background-color: #d3be6c; font-family: Arial,
Helvetica, sans-serif; font-size: 13px; shade: #000000;
padding: 0px 15px 10px 15px;">
<div type="font-size: 48px; shade:blue;">
<b>StackAbuse</b>
</div>
<div type="font-size: 24px; shade: #555100;">
<br> Sending E-mail utilizing Spring Cloud with <b>FreeMarker</b> template !!! <br>
</div>
<div>
<br> Wish to be taught a brand new know-how or grow to be an in-demand full-stack developer?<br>
<br> We train the talents you must degree up in your profession.<br>
<br>"Sharing data is the largest studying" <br> <br>
<br> <br> <b>${Identify}</b><br>${location}<br>
<br>
</div>
</td>
</tr>
</desk> <br> <br></td>
</tr>
</desk>
</physique>
</html>
Subsequent, we have to outline a configuration class to load the template from the trail and add as bean. For this, we are going to outline FreeMarkerConfigurationFactoryBean
:
@Configuration
public class FreemarkerConfig {
@Major
@Bean
public FreeMarkerConfigurationFactoryBean factoryBean() {
FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
bean.setTemplateLoaderPath("classpath:/templates");
return bean;
}
}
Subsequent, we are going to outline our service layer to load this template and create a closing message to ship to SES:
@Service
public class EmailService {
@Autowired
personal JavaMailSender javaMailSender;
@Autowired
personal Configuration config;
public void sendTemplateMessageWithAttachment(SimpleMailMessage simpleMailMessage) {
strive {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(
message,
MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
StandardCharsets.UTF_8.title());
Template t = config.getTemplate("email-template.ftl");
Map<String, Object> mannequin = new HashMap<>();
mannequin.put("Identify", "StackAbuse Admin");
mannequin.put("location", "Bangalore, India");
String html = FreeMarkerTemplateUtils.processTemplateIntoString(t, mannequin);
helper.addAttachment("brand.png", new ClassPathResource("brand.png"));
helper.setTo(Objects.requireNonNull(simpleMailMessage.getTo()));
helper.setText(html, true);
helper.setSubject(Objects.requireNonNull(simpleMailMessage.getSubject()));
helper.setFrom(Objects.requireNonNull(simpleMailMessage.getFrom()));
javaMailSender.ship(message);
} catch (MessagingException | IOException | TemplateException e) {
System.err.println("Exception: " + e.getMessage());
}
}
Lastly, we are going to outline a Controller
layer to move the dynamic e mail attributes:
@RestController
public class EmailController {
@Autowired
personal EmailService emailService;
@PostMapping("/sendTemplateEmailWithAttachment")
public String sendTemplateMessageWithAttachment(@RequestBody EmailDetails emailDetails) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(emailDetails.getFromEmail());
simpleMailMessage.setTo(emailDetails.getToEmail());
simpleMailMessage.setSubject(emailDetails.getSubject());
simpleMailMessage.setText(emailDetails.getBody());
emailService.sendTemplateMessageWithAttachment(simpleMailMessage);
return "E-mail despatched efficiently";
}
}
Now, if we run the applying and execute the next curl it’ll ship an e mail to the verified e mail handle:
curl -i -X POST
-H "Content material-Sort:software/json"
-d
'{
"fromEmail": "[email protected]",
"toEmail": "[email protected]",
"topic": "check e mail",
"physique": "Hello, This can be a check template e mail with attachment."
}'
'http://localhost:8080/sendTemplateEmailWithAttachment'
Subsequent, we will login to the recipient’s mail handle and confirm if the recipient has acquired the e-mail:
Sending Personalised E-mail utilizing Templates in AWS SES
Within the earlier use-case we used a static template to ship emails. How can we allow templates to be designed dynamically for various functions and several types of recipients? AWS SES permits us to create e mail templates to ship customized emails to a number of locations in a single operation.
We will create as much as 10,000 e mail templates per Amazon SES account. Every template might be as much as 500KB in measurement, together with each the textual content and HTML elements. We will ship upto 50 locations in every name.
So let’s rapidly create an e mail template. First, we will outline a JSON file utilizing the next template:
{
"Template": {
"TemplateName": "MyTemplate",
"SubjectPart": "Greetings from {{title}}!",
"HtmlPart": "<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content material-Sort" content material="textual content/html; charset=utf-8"><title>StackAbuse E-mail</title></head><physique><desk width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="heart" valign="high" bgcolor="#838383" type="background-color:#838383"><br><br><desk width="600" border="0" cellspacing="0" cellpadding="0"><tr><td align="heart" valign="high" bgcolor="#d3be6c" type="background-color:#d3be6c;font-family:Arial,Helvetica,sans-serif;font-size:13px;shade:#000;padding:0 15px 10px 15px"><div type="font-size:48px;shade:#00f"><b>StackAbuse</b></div><div type="font-size:24px;shade:#555100"><br>Sending E-mail utilizing Spring Cloud with AWS SES E-mail template !!!<br></div><div><br>Wish to be taught a brand new know-how or grow to be an in-demand full-stack developer?<br><br>We train the talents you must degree up in your profession.<br><br>"Sharing data is the largest studying"<br><br><br><br><b>{{title}}</b><br>{{location}}<br><br></div></td></tr></desk><br><br></td></tr></desk></physique></html>",
"TextPart": "Pricey {{title}},rnHere is your StackAbuse E-mail."
}
}
This template comprises the next attributes:
- TemplateName: This comprises the title of the template.
- SubjectPart: This holds the e-mail’s topic line. Alternative tags may be current on this asset. These tags are formatted as follows:
{{tagname}}
. You’ll be able to enter a worth for{{tagname}}
for every vacation spot when sending the e-mail. - HtmlPart: This comprises the HTML physique of the e-mail and it could possibly additionally comprise alternative tags.
- TextPart: This represents the e-mail’s textual content physique. This model of the e-mail is distributed to recipients whose e mail purchasers don’t view HTML emails. Alternative tags may be current on this asset.
We will save this file as mytemplate.json
. Lastly we will use an AWS CLI command to create the template as follows:
$ aws ses create-template --cli-input-json file://mytemplate.json
Subsequent, let’s outline a service layer to outline attributes and ship templated emails:
@Service
public class EmailService {
@Autowired
personal AmazonSimpleEmailService simpleEmailService;
public void sendTemplatedMessage(SimpleMailMessage simpleMailMessage) {
Vacation spot vacation spot = new Vacation spot();
Listing<String> toAddresses = new ArrayList<>();
String[] emails = simpleMailMessage.getTo();
Collections.addAll(toAddresses, Objects.requireNonNull(emails));
vacation spot.setToAddresses(toAddresses);
SendTemplatedEmailRequest templatedEmailRequest = new SendTemplatedEmailRequest();
templatedEmailRequest.withDestination(vacation spot);
templatedEmailRequest.withTemplate("MyTemplate");
templatedEmailRequest.withTemplateData("{ "title":"StackAbuse Admin", "location": "Bangalore, India"}");
templatedEmailRequest.withSource(simpleMailMessage.getFrom());
simpleEmailService.sendTemplatedEmail(templatedEmailRequest);
}
}
We will add a number of Vacation spot
addresses to ship bulk emails to a number of recipients. We’re utilizing the sendTemplatedEmail()
technique from the AmazonSimpleEmailService
interface to ship this templated e mail. We additionally must move the alternative tags to get replaced within the HTML textual content of our template that we created earlier.
Lastly, we are going to outline a Controller
layer to outline the REST API to move the attributes:
@RestController
public class EmailController {
@Autowired
personal EmailService emailService;
@PostMapping("/sendAWSTemplatedEmail")
public String sendTemplatedMessage(@RequestBody EmailDetails emailDetails) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(emailDetails.getFromEmail());
simpleMailMessage.setTo(emailDetails.getToEmail());
simpleMailMessage.setSubject(emailDetails.getSubject());
simpleMailMessage.setText(emailDetails.getBody());
emailService.sendTemplatedMessage(simpleMailMessage);
return "E-mail despatched efficiently";
}
}
Subsequent, once we run the app, we will execute the next curl
to ship templated emails:
curl -i -X POST
-H "Content material-Sort:software/json"
-d
'{
"fromEmail": "[email protected]",
"toEmail": "[email protected]",
"topic": "Greetings from StackAbuse Admin",
"physique": "Hello, This can be a AWS templated e mail."
}'
'http://localhost:8080/sendAWSTemplatedEmail'
Now the recipient lastly will get to see a templated e mail:
Request Manufacturing Entry
Lastly, with a view to ship emails to any recipient, no matter whether or not the receiver’s handle or area is validated, we should in the end take our account out of the sandbox. All of our identities, together with From
, Supply
, Sender
, and Return-Path
addresses, should nonetheless be verified. We will submit a request for Manufacturing entry from the “Account Dashboard” web page as follows:
We will submit the request filling up all of the above particulars from AWS Console. The identical will also be submitted utilizing AWS CLI. This may be useful when we have to request entry for numerous identities and wish to automate the method.
Conclusion
The important thing concepts of Amazon Easy E-mail Service (SES) and the libraries provided by Spring Cloud AWS to interface with it have been coated on this article. Moreover, we created a Spring Boot software with a REST API that may ship emails by the Spring Cloud AWS SES module.
You must now have a stable understanding of what Amazon Easy E-mail Service (SES) is and put it to use to ship emails.