Java Mail Framework using Spring Boot – Step by Step Guide With Source Code

Every application be it desktop or web, needs to send some kind of notification via mail. The scenarios may differ as per requirements like registration mail, order confirmation or any batch job completion.

Java Mail Framework using Spring Boot

In this article, we will see a 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 emails. You can download source 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 the 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 form template that is wrapped in {{var_name}} with the appropriate value from the Map you have passed to it.

Now let’s check the SMTP configuration part. As we are using Spring Boot, we don’t have to do anything 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 a 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 our 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 asJava Mail framework using Spring Boot.

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

Isn’t it easy? Go ahead and download the code from our Github and use it.

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

14 Comments
  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 *