Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

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>
    <groupId>com.sendgrid</groupId>
    <artifactId>sendgrid-java</artifactId>
    <version>4.10.1</version>
</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
sendgrid.sender.email=no-reply@yourdomain.com

 

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 org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class SendGridService {

    @Value("${sendgrid.api.key}")
    private String sendGridApiKey;

    @Value("${sendgrid.sender.email}")
    private String senderEmail;

    public void sendEmail(String to, String subject, String contentHtml) throws IOException {
        Email from = new Email(senderEmail);
        Email recipient = new Email(to);
        Content content = new Content("text/html", contentHtml);
        Mail mail = new Mail(from, subject, recipient, content);

        SendGrid sg = new SendGrid(sendGridApiKey);
        Request request = new Request();

        try {
             request.setMethod(Method.POST);
             request.setEndpoint("mail/send");
             request.setBody(mail.build());
             Response response = sg.api(request);

             // Log the response
             System.out.println("Status Code: " + response.getStatusCode());
             System.out.println("Body: " + response.getBody());
             System.out.println("Headers: " + response.getHeaders());

        } catch (IOException ex) {
             // Handle the exception (e.g., log it)
             throw 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]

Written by
Author Avatar
Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

COMPANY PROFILE

Please check out our Company Profile.

Download

COMPANY PORTFOLIO

Explore my work!

Download

ASK ISB Vietnam ABOUT DEVELOPMENT

Let's talk about your project!

Contact US