Spring Boot Freemarker Email Template

This Spring Boot Freemarker Email Template tutorial walks you through sending the email with freemarker template using Spring Boot. This topic will cover, how to setup maven project, email service implementation, load freemarker template, Spring Boot application configuration.
If you are newbie on the Spring Boot and Freemaker, you may refer to the post Spring Boot FreeMarker Hello World Example before starting this topic.

Other interesting posts you may like

Let’s begin:

Project structure
In this Spring Boot Freemarker Email Template tutorial, we will create a project with directory structure like below

Spring Boot Freemarker Email Template

Maven Dependencies
To create Spring Boot Freemarker Email Template example, you need to add dependencies into pom.xml file like below:

Mail Service implementation
Our Mail service is implemented such as the below class

Dig deeper: The mail service is annotated as “mailService” using @Service annotation. Beside, we use @Autowired annotation to autowire JavaMailSender and free marker configuration beans.

Spring MimeMessageHelper class provides support for HTML text content, inline elements such as images, and typical mail attachments. It also takes care of populating MimeMessage object.

processTemplateIntoString() method replaces the placeholders values with model attribute values and populates email contents from FreeMarker template.

JavaMailSender.send() method to send populated MIME message.

Configuration properties

Fetch email properties into EmailProperties bean

Setup Mail sender
Create mail sender using Spring JavaMailSenderImpl class

Mail Pojo
This is simple pojo class which contains different email attributes like mailFrom, mailTo, mailCc, mailBcc, mailSubject, mailContent, contentType, attachments and model.

FreeMarker Email Template
Create freeMarker email template file (email-template.ftl) under folder /resources/templates/ along with respected placeholders variables.

Create Spring Boot Application
We create Spring Boot Application class which implements CommandLineRunner and has the main method

Run the Spring Boot Application
Foremost, we need to go to project directory in console, then package and run it using command line.

It sends email to specified recipients. Here is the email which i have received

That’s all on the Spring Boot Freemarker Email Template tutorial.
If you found this useful, please share it with your friends

References
Sending email
Spring Boot FreeMarker

Download complete source code, click link below

spring-boot-freemarker-email-template-1.zip (75 downloads)

2 thoughts on “Spring Boot Freemarker Email Template

  1. Hi Im getting below error,

    501 5.5.4 Invalid Email address
    DEBUG SMTP: got response code 501, with response: 501 5.5.4 Invalid Email address

    RSET
    250 2.0.0 Resetting
    DEBUG SMTP: MessagingException while sending, THROW:
    com.sun.mail.smtp.SMTPSendFailedException: 501 5.5.4 Invalid Email address
    ;
    nested exception is:
    com.sun.mail.smtp.SMTPSenderFailedException: 501 5.5.4 Invalid Email address

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2267)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1758)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1257)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:448)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
    at com.javabycode.service.MailService.sendEmail(MailService.java:40)
    at com.javabycode.SpringBootEmailApplication.run(SpringBootEmailApplication.java:40)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
    at com.javabycode.SpringBootEmailApplication.main(SpringBootEmailApplication.java:22)
    Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 501 5.5.4 Invalid Email address

    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1767)
    … 13 more
    QUIT
    221 2.0.0 BLU437-SMTP9.smtp.hotmail.com Service closing transmission channel
    2017-08-27 16:48:27 ERROR o.s.boot.SpringApplication – Application startup failed
    java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
    at com.javabycode.SpringBootEmailApplication.main(SpringBootEmailApplication.java:22)
    Caused by: org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 501 5.5.4 Invalid Email address
    ;
    nested exception is:
    com.sun.mail.smtp.SMTPSenderFailedException: 501 5.5.4 Invalid Email address

    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:474)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
    at com.javabycode.service.MailService.sendEmail(MailService.java:40)
    at com.javabycode.SpringBootEmailApplication.run(SpringBootEmailApplication.java:40)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776)
    … 6 common frames omitted

Leave a Comment

*

Please share it if you found this useful
Hide Buttons