In modern web applications, sending email is a fundamental function—from account confirmation emails and password resets to system notifications. However, if you try to send emails via a regular SMTP server, you often encounter problems such as:
- Emails being marked as spam
- Difficulty in tracking delivery status
- Sending rate limits
The solution? SendGrid—a professional, stable, and easy-to-integrate email service. In this article, we'll learn how to integrate SendGrid into a Spring Boot application to send emails quickly and securely.
I. What is SendGrid?
SendGrid (owned by Twilio) is a well-known cloud-based email delivery service that supports:
- Sending transactional emails
- Sending marketing emails
- Managing recipient lists and email templates
- Tracking open rates, bounce rates, and click tracking
SendGrid offers a free plan that allows sending 100 emails/day, which is perfect for testing or small projects.
II. Create an Account and Get an API Key
- Go to https://sendgrid.com
- Click Start for Free to register an account.
- After verifying your email, log in to the dashboard.
- Navigate to Settings → API Keys.
- Click Create API Key.
- Set a name (e.g., springboot-mail-key) and choose Full Access permissions.
- Save the API Key (Important: It is only displayed once).
III. Add the Dependency to pom.xml
Add the official SendGrid Java library to your project's pom.xml:
|
XML <dependency> |
IV. Configure the API Key
Add your SendGrid API key and default sender email to your application.properties file:
|
Properties sendgrid.api.key=SG.xxxxxxxx_your_key_here |
V. Create the SendGridService to Send Email
Next, create a service class that will handle the email sending logic. This class will read the configuration values using @Value and use the SendGrid library to make the API call.
|
Java package com.example.mail.service; import com.sendgrid.*; import java.io.IOException; @Service @Value("${sendgrid.api.key}") @Value("${sendgrid.sender.email}") public void sendEmail(String to, String subject, String contentHtml) throws IOException { SendGrid sg = new SendGrid(sendGridApiKey); try { // Log the response } catch (IOException ex) { |
Conclusion
With just a few configuration steps, you can now:
- Create a SendGrid API key
- Integrate the SendGrid SDK into Spring Boot
- Send professional HTML emails
- Track the delivery status on the SendGrid Dashboard
SendGrid is an excellent choice if you want reliable email delivery, to avoid spam filters, and have flexible scalability for your application.
Whether you need scalable software solutions, expert IT outsourcing, or a long-term development partner, ISB Vietnam is here to deliver. Let’s build something great together—reach out to us today. Or click here to explore more ISB Vietnam's case studies.
[References]
- https://www.freepik.com/free-photo/email-messages-network-circuit-board-link-connection-technology_1198384.htm#fromView=search&page=2&position=5&uuid=bac2f95d-fed2-4913-ad10-ed38e4d79103&query=email (Image link)
- https://github.com/sendgrid/sendgrid-java
- https://www.twilio.com/docs/sendgrid
- https://spring.io/projects/spring-boot









