Member-only story
How to send Template based Email using Spring Boot and FreeMarker.
Hi all, In this Tutorial, i will be talking about template based Email using spring boot application.

To implement Email Functionality with spring boot We require below dependencies .
- FreeMarker (As we are going to use FreeMarker template for our Email Html and content)
- JavaMail Api(Api for sending java mails).
Whole source code can be found on my github repository
Lets first define and create Models, controller and other classes which we require to send email.
- EMail.java , A simple Pojo for having To,From,model and content to send mail.
- MailSender.java , A Service to send email
- emailtemplate.flth
First thing we have to define dependency of javamail, which we can do in spring boot using spring-boot-starter-mail.
it defines all required dependencies for mail.
in pom.xml
<!-- send email -->
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
in application.properties, We have to define email provider properties to send the mail. For Free Generic email provider we can use Gmail to send our mail.
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=username
spring.mail.password=password
# Other properties
spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=5000 spring.mail.properties.mail.smtp.writetimeout=5000 # TLS , port 587 spring.mail.properties.mail.smtp.starttls.enable=true
Sending normal text mail
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender; public class MailSender {
@Autowired…