email spring heroku sendgrid

Java Spring On Heroku Email Service With SendGrid

As a developer, don’t you just love it when integrating frameworks works out of the box, the first time? I do, and that is exactly what happened a couple of days ago with my Java Spring Data REST application deployed on Heroku, when I wanted to implement an email service for my server.

I surveyed the Heroku Elements Marketplace for an email add-on that could provide the required email capability. I looked briefly at the numerous options, and I liked what I saw with SendGrid, so I chose it as my email backing service.

So far, that choice has been a very good one. Within about 15 minutes after making the decision, I had installed SendGrid on Heroku, and I had my initial Java service class for sending emails written. I tested sending an email based on a REST request by using Postman, to emulate what I’ll be doing from my iOS mobile app. Soon after sending the REST request, I saw the email in my test account. Awesome!

The documentation for integrating SendGrid on Heroku is very clear and helpful. Add to that the fact that SendGrid has a free starter plan that allows up to 12,000 emails per month (at the time of writing this post), and you’ve got a great product to fulfill your email service needs.

This won’t be a long post, and it won’t have tons of code. However, when something is so easy to implement, I think it deserves mention.

I already had my server application deployed on Heroku. Integrating the email service just required following the steps listed here on Heroku for SendGrid.

As the instructions illustrate, you setup the API key in the Heroku environment using the following command.

From that point on, you can reference the SENDGRID_API_KEY variable in your java code.

If you are using Spring Boot like I am, you only need to add two dependencies to implement the email service. spring-boot-starter-mail provides an easy abstraction for sending email using the JavaMailSender interface, and Spring Boot provides auto-configuration for it as well as a starter module. The other dependency is for SendGrid. I’m using Gradle, so in my build.gradle I added the two lines below to my dependencies.

I implemented an EmailService class to handle sending emails when required. Here is the initial definition that I used for testing that everything was up and running.

You would probably want to send emails with a name and email address, instead of just an email address as the from Email. In that case, you would create the Email instance with both parameters as shown below.

I was super excited when I received an email at a real email address I used in the test. I’ve since modified my sendEmail method to accept parameters and be more robust, but if you are just getting started, you can prove out the integration with a test method similar to what I’ve shown above.

Thanks SendGrid and Heroku for making this so easy!

Java and Spring … well, thanks yet again 🙂

Leave a Comment: