Spring Mail – Sending Email with Freemarker HTML Template Example

You may also like...

Subscribe
Notify of
guest
9 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Chris
Chris
March 16, 2018 07:01

Thank you, this was very helpful!

I noticed your example code didn’t work out of the box for me, but after two small adjustments it was fine:

  • In EmailService.java, we had to use helper.addInline("logo.png", new ClassPathResource("memorynotfound-logo.png")); instead of addAttachment.
  • The addInline call needed to be moved after the setText call, instead of before it.

After this, we got emails coming out with attachments shown inline just as in your example.

Kent
Kent
March 19, 2018 23:56

This guide could be improved. If using helper.addInline versus helper.addAttachment make sure to call helper.setText before any calls to addInline otherwise the image will not be properly added.

gqrdqqfd
gqrdqqfd
May 25, 2018 07:26

mail.getModel() is not defined

bgdc
bgdc
October 4, 2018 17:35
Reply to  gqrdqqfd

mail.setModel() is not defined too

Ibrahim AlTamimi
Ibrahim AlTamimi
October 8, 2018 01:24
Reply to  gqrdqqfd

Map model = new HashMap();
model.put(“name”, “Memorynotfound.com”);
model.put(“location”, “Belgium”);
model.put(“signature”, “https://memorynotfound.com”);

Tùng
Tùng
January 21, 2019 18:04

I got error : “javax.mail.AuthenticationFailedException: 454 4.7.0 Too many login attempts, please try again later…”
What should I do? :((

Gustavo Cinque
Gustavo Cinque
March 29, 2019 17:41

Hi! Seems to be a nice way to load html files. I’m facing a little problem, though. It seems like none of my ftl files are being cached. At one point, after failing to retrieve the ftl file from cache, the code goes and searches for a _.ftl, but even with a file corresponding that name in the correct folder it doesn’t work. Inside the Configuration#createTemplateCache() method, the loader points to the correct templateLoaderPath set on the FreemarkerConfig. One question, is the folder hierarchy correct in this example? Because mine is exactly the same, even the folder’s name. Error: java.io.FileNotFoundException:… Read more »

Arjun
Arjun
April 14, 2023 08:02

Hi, I am not getting Image in Mail content, Attachment are working,
Please see attached Image

9
0
Would love your thoughts, please comment.x
()
x