Amazon SES & Spring Boot Integration
2 min readAug 23, 2020
Send emails using AWS SES and spring boot.
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-mailerIf 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.