Java Mail Framework using Spring Boot

Every application be it desktop or web, needs to send some kind of notification via mail. The scenarios may differ as per requirement like registration mail, order confirmation or any batch job completion. In this article we will see simple but powerful Java Mail sending framework using Spring Boot.

Software used for this example

  • Java 1.8
  • Spring Boot 1.5.2.RELEASE
  • FakeSMTP
  • Eclipse

As mentioned above we will be using Spring Boot Mail to provide all the boilerplate code and configuration. Below is the pom file entry for mail

We will simply build the simple template system on top of that. This Java Mail Framework will be able to send HTML as well as Plain text mails. You can download soruce code from here.

As a basic requirement for any mail we need few details like to, from, subject, message. To hold this information we will define a simple java bean as below

You can see that we have added some convenience constructors. After this we will define a template class which will help us to load the required html or text template and prepare message body out of it. This class will also provide dynamic variable replacement in the message body.

You can see that the class accepts and template ID which it tries to load from /resources/email-templates folder. Here is the project structure.Java Mail Demo - Project Structure

Also you can see the utility method which gives us the message body after replacing each variable from template that is wrapped in {{var_name}} with appropriate value from the Map you have passed to it.

Now lets check the SMTP configuration part. As we are using Spring Boot, we don’t have to do any thing except specifying the correct server details in application properties and use the JavaMailSender in your application. We have below details in our file

If you are wondering how SMTP is working on the localhost then its magic of FakeSMTP. It provides simple runnable jar that works as our local SMPT server and we can test mails from our dev system. Once you download and run the jar on your machine it will look like this.

Here is out Email Service which provides utility methods to send HTML or PLAIN TEXT mails.

Below is the code to send mail using our template system.

and here is our template, you can see how we have used the replacement variables and replaced them dynamically while sending mail. Plain text templates will be similarly prepared and processed.

Once you run the application you will see an email entry immediately in your FakeSMTP server as

You can double click the mail and see how it looks. Check below Tadaaa!!!!! Its your HTML mail

Isnt it easy? Go ahead and download the code from our Github and use it.

Please getback to me if you see any issue in the code or need help of some sort.

  1. Yagué
    August 13, 2018 | Reply
    • Pavan
      August 17, 2018 | Reply
  2. Meghana Mohite
    August 7, 2018 | Reply
  3. madhuleena
    July 24, 2018 | Reply
    • Pavan
      July 27, 2018 | Reply
  4. gunji
    May 2, 2018 | Reply
    • Pavan
      May 3, 2018 | Reply
  5. Meghana Mohite
    April 20, 2018 | Reply
  6. April 3, 2018 | Reply
    • Pavan
      April 4, 2018 | Reply
  7. Alex Bosker
    November 24, 2017 | Reply
  8. October 23, 2017 | Reply

Leave a Reply to Meghana Mohite Cancel reply

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