Tips && Tricks

Send Mail using Spring Boot

29 Jun 2016

Configuring and sending emails in java has become a lot more easier with the support of Spring Boot. Now you can send emails in your application with very little configuration and development time.

Below are the steps that shows how this can be achieved.

Step 1 - Add spring boot mail dependency in pom.xml

Spring Boot has encapsulated all the mail related dependencies into a starter module and we just have to add this starter dependency in our project pom xml.


Step 2 - Define a few application properties

Spring Boot provides a lot of ready to use application properties for configuring the application easier and faster. For supporting mails, it provides properties of the format spring.mail.* (used for basic configuration) and* (used for further configuring the java mail properties)

Listed below are the very minimal configuration required. It uses the gmail as the smtp host provider.

Step3 - Email Sender Service

Spring boot provides a default implementation of JavaMailSender which can be autowired and used to send the mail. This default implementation is skipped/ignored if the app developer decides to have a custom implementation of JavaMailSender.

Below code uses(via autowire) the default JavaMailSender implementaion provided by Spring Boot.

    private JavaMailSender javaMailSender;
    public void sendMail(String to, String subject, String body) {
        SimpleMailMessage message = new SimpleMailMessage();

Step4 - Special case if using gmail SMTP

If you decide to go with gmail smtp, you will also have to allow your google account to support less secure apps. Google disables this support by default to restrict users from sending mails from places other than google-logged-in browsers and google-apps.

To turn on this, go to

And you finally have a simple mail sending service. Enjoy!

comments powered by Disqus