Send E-Mails with SendGrid and Spring Boot

Last Updated:  June 15, 2020 | Published: July 8, 2018

In the last weeks, I was searching for a nice solution to send emails to users for one of my side projects. Sending plain text formatted emails with the Spring Boot Mail Starter is quite easy as it is an abstraction using the JavaMailSender interface and auto-configured. It's also possible to send HTML formatted emails or use a template engine like Thymeleaf to generate the content. With this setup, I had to generate different email templates by myself using my editor. As email design (especially with HTML) is not one of my main strengths I took a lot of time and I started searching for a solution where the “business” could template the emails by themselves with a WYSIWYG as they have no deeper HTML knowledge.

For this task, I found a service called SendGrid. With SendGrid you can create nice looking email templates using a web editor with drag-and-drop functionality. You can also define variables within your email templates to e.g. add the username or a link to the body. Another nice benefit of SendGrid is that it is integrated into the most Cloud Providers (e.g. Azure, Heroku, Pivotal Web Services …) and with the free version of SendGrid, you can cover most of the use cases of a small to a mid-sized project.

The built-in email editor gives you the opportunity to add your own HTML code or to use some prebuild modules for your email e.g. a button, social media icons and a unsubscribe link. Variables for your email are surrounded with –VARIABLE_NAME- :

 

To reproduce the following code example you need an own SendGrid account and you have to create an API key to communicate with the internal SendGrid API.

Spring Boot project setup

For a quick example, I'll create a Spring Boot web project with one REST controller which will trigger sending an email. The pom.xml for this project looks like the following:

If you have spring-boot-autoconfigure on your classpath which is a transitive dependency of e.g. spring-boot-starter-web you just have to add your API key to your application.properties file and Spring Boot will take care of creating the central bean for the communication with the SendGrid API:

Sending e-mails with the SendGrid SDK

The REST endpoint looks like the following:

The central communication interface is the SendGrid bean which offers a nice programming API for sending emails. The substitution of variables is achieved with the following line of code:

To address the right email template you have to set the template ID correctly. A simple HTTP GET call in the browser like http://localhost:8080/sendgrid?msg=Hello%20World will send the following email:

You can find the full code example on GitHub. To use it locally you have to replace the SendGrid API key and the template id in the application.properties file.

Find further e-mail related blog posts here.

Happy email sending with SendGrid and Spring Boot!

Phil

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Sign up for Our Mailing List And Get

    the Testing Java Applications ($9) Cheat Sheet for Free

    Testing Java Applications Cheat Sheet Cover
    >