This example shows how to send email in a Spring Boot application. We use a Mailtrap service as SMTP server [We need to register an account if we do not have Mailtrap account]
+ Java 11
+ Spring Boot 2.5.1.RELEASE
+ Feemarker
+ Spring Boot Rest API
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
spring.mail.protocol=smtp
spring.mail.host=smtp.mailtrap.io
spring.mail.port=2525
spring.mail.username=<mailtrap_username>
spring.mail.password=<mailtrap_password>
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.starttls.enable = true
Create your email.ftlh file under src/main/resources/templates as below.
<html>
<head></head>
<body>
<p>Dear ${name},</p>
<p>Sending Email using Spring 4 with <b>FreeMarker template !!!</b></p>
<p>Thanks</p>
</body>
</html>
For further reference, please consider the following sections:
- Official Apache Maven documentation
- Spring Boot Maven Plugin Reference Guide
- Java Mail Sender
- Apache Freemarker
The following guides illustrate how to use some features concretely: