Amazon SES & Spring Boot Integration

In this article, I will show you the steps needed to send emails from a Boot Spring app using Amazon SES or any other SMTP service.Spring boot provides all the configuration of JavaMail by a single starter dependency. Add this following dependency to your spring boot pom.xml file.<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><version>2.2.5.RELEASE</version></dependency>Once the dependency is in place, the next step is to specify the AWS SES properties in the application.properties file using the spring.mail.* namespace.###################AWS SES PROPERTIES################spring.mail.host=<host>>
spring.mail.username=<user>>
spring.mail.password=<password>>
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.port=587
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.trust=*
When you are trying this in your local or in a non-ssl or untrusted SSL server. You may get unable to connect to host exception. So add the last linespring.mail.properties.mail.smtp.ssl.trust=*This will ignore the ssl check.To get the host , username and password. Login to your AWS console and go to ses SMTP Settings and generate your credencial.https://github.com/amaresh8053/springboot-smtp-mailer/blob/master/Screenshot_31.pngNow Make a service class(SmtpService.Java) in your project and follow this sample code.@Service
public class SmtpService {

@Autowired
private JavaMailSender javaMailSender;
public void send(Request request){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(request.getFrom());
message.setTo(request.getTo());
message.setSubject(request.getSubject());
message.setText(request.getBody());
javaMailSender.send(message);
}
}
Create a sample Model for your email payload(Request.Java)private String to;
private String body;
private String from;
private String subject;
Now you can able to send emails. Find the comple project here
https://github.com/amaresh8053/springboot-smtp-mailer
If you want to learn bulk email, email marketing and how to create your own email marketing system using open source stack, get this ebook www.amazon.com/dp/B08C7VT5PF. I have covered all basics with step by step method.

--

--

--

Another DevSec Guy.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Project Euler #5: Smallest multiple — Hacker Rank Program

Dynamic Programming: “Memoization”

Milky Way

Automate Exploratory Testing in Android — Build your own crawler

TPS and Agile

east-west.jpg

Measurement Must Have A Purpose

Random-Access (Seekable) Streams for Amazon S3 in C#

Uploading files to Azure Blob Storage in PHP

Connection between C# and MYSQL

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amaresh Pattanayak

Amaresh Pattanayak

Another DevSec Guy.

More from Medium

Java Libraries

Develop Bluetooth LE application easily with JAVA and BleuIO

Migrating from Mockito 1 to Mockito 2 on Java 11

Marshaling and Unmarshaling XML using JAXB