SendGrid emails in Spring Boot

Carvia Tech | August 23, 2020 | 3 min read | 577 views

In this tutorial we will learn how to send sendgrid text email, multimedia email, email with attachment using sdk from Java and Spring Boot applications.

SendGrid emails with Spring Boot

Spring Boot has auto-configuration (org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration) for SendGrid that activates when you include the sendgrid java library in build.gradle and specify the below property in application.yml.

implementation 'com.sendgrid:sendgrid-java:4.6.1'

You can find the latest version of sendgrid-java from

# SENDGRID CONFIG (SendGridAutoConfiguration)
spring.sendgrid.api-key: <API-KEY> # SendGrid API Key

spring.sendgrid.username: # SendGrid account username (if API key not provided).
spring.sendgrid.password: # SendGrid account password. # SendGrid proxy host. (optional)
spring.sendgrid.proxy.port: # SendGrid proxy port. (optional)

Spring boot will automatically create a Bean of type SendGridAPI which you can autowire into your component for sending emails.
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGridAPI;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;


public class SendGridMailer {

    private SendGridAPI sendGridAPI;

    void sendMail() {
        Email from = new Email("", "Foo Bar");
        String subject = "Test email with SendGrid";
        Email to = new Email("");
        Content content = new Content("text/plain", "Test email with Spring");
        Mail mail = new Mail(from, subject, to, content);
        Request request = new Request();
        try {
            Response response = sendGridAPI.api(request);
        } catch (IOException ex) {

Attachment support

Sendgrid support attachments with email, more details are provided in this article.

Dynamic Templates

Using dynamic templates, we can create rich text emails in sendgrid and substitute variables from Java program. More details in this article.

SendGrid Emails in Java without Spring

We can use SendGrid without Spring framework, by managing our own instance of SendGrid created with right SendGrid API Key.

Plain Java implementation for SendGrid
// using SendGrid's Java Library

import com.sendgrid.*;


public class Example {

    public static void main(String[] args) throws IOException {
        Email from = new Email("");
        String subject = "Sending with SendGrid is Fun";
        Email to = new Email("");
        Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
        Mail mail = new Mail(from, subject, to, content);
        SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
        Request request = new Request();
        try {
            Response response = sg.api(request);
        } catch (IOException ex) {
            throw ex;

Did you know?

You can use Unix command line utility to encode and decode in base64 format.

Encode into Base64
$ echo  '' | base64
Decode Base64 content
$ echo 'bGludXhoaW50LmNvbQo=' | base64 --decode

That’s all.

Top articles in this category:
  1. Sendgrid Dynamic Templates with Spring Boot
  2. SendGrid Attachments with Spring Boot
  3. Redis rate limiter in Spring Boot
  4. Setting a Random Port in Spring Boot Application at startup
  5. Basic Auth Security in Spring Boot 2
  6. Testing web layer in Spring Boot using WebMvcTest
  7. Feign RequestInterceptor in Spring Boot

Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info