Integrate SendGrid With a Spring Boot and Java App

In this post, you will learn how to integrate your Spring Boot and Java app with the SendGrid Web API. The following are some of the points covered:

* Create and Configure/Load the SendGrid API Key.
* Configure the SendGrid Maven entry in POM.xml.
* Use the SendGrid EmailService custom implementation.
* Invoke/Test the Email Service.

Create and Configure/Load the SendGrid API Key

* Create an account with SendGrid.com
* Go to the SendGrid Setup Guide to using the Web API page. Create an API key.
* Execute the following script in your development environment. echo “export SENDGRID_API_KEY=‘Paste_Your_API_Key’” > sendgrid.env echo “sendgrid.env” >> .gitignore source ./sendgrid.env

* Alternatively, you could also configure the SendGrid API key in your application.properties file and read the key details as part of loading the configuration beans. The following is the sample code: import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import com.sendgrid.SendGrid; @Configuration @PropertySource(“classpath:application.properties”) public class SendGridConfig { @Value(“${sendgrid.api.key}”) String sendGridAPIKey; return new SendGrid(sendGridAPIKey); } }In the above code sample, the SendGrid API Key is read from the application.properties file. The following is the content of the application.properties file. sendgrid.api.key = ABCcvbdTYDRS_ksjdhIUYICS.-836jksjsdhYTkPt12hdgsT-whdk ahdjfpT3shdBCVNS

Make sure that you put the API key and not the API Key Id. Or, else, you would get an exception notifying you that the SendGrid API Key not working, such as,“The provided authorization grant is invalid, expired or revoked.” https://goo.gl/RH7b9e #DataIntegration #ML