Send Email Spring FreeMarker

Send Email Spring FreeMarker

In previous example you saw how to send email using Spring API without doing much coding. In this example I will show you how to use FreeMarker java template engine to create html template and send data in HTML format. You will be needed this functionality if you want to send email in HTML format.

Tools needed:

  • Eclipse any latest version
  • JDK 8
  • FreeMarker template jars
  • Log4j jars

To understand better I will create complete maven project and show you how initial set up can be done and finally will send sample email using this application. Create maven project name: SpringSendEmailFreeMarker as shown below and include all files and folders:

Send Email Spring FreeMarker

  • As first thing you need to do is include all dependencies in pom.xml:

Now include application configuration files inside src\main\resources folder:

  • spring-context.xml: Main file of the application as we are using Spring framework and here we keep all bean defintions, properties configuration load etc…

  • log4j2.xml: Used for application logging:

  • JavaHonkFreeMarkerTemplate.ftl: FreeMarker template is which will be use to format and send eamil in HTML format:

  • Central location to define application related properties and use through out the application:

  • This is main class which will be use to send email:

  • Java main class to load application and send emails:

  • That’s it. If you have SMTP server setup you could change it in file for and if you don’t have any SMTP server for test please download and use FakeSMTP from here as I will also use this FakeSMTP server to send an email. To run just start FakeSmtp server then run SendEmailUsingFreeMarker as java application. If all setup done correctly you should see below emails in FakeSMTP server as below:

Send Email Spring FreeMarker

  • All the email are accessible for this example we have to verify if FreeMarker template generated email or not. Open first email you will see below email formatted by FreeMarker:

Send Email Spring FreeMarker

download Download Project: SpringSendEmailFreeMarker

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *