SendGrid Attachments with Spring Boot

Carvia Tech | July 28, 2020 | 3 min read | 0 views

SendGrid allows us to attach files with email using base64 encoded format and, it also provides a helper method to seamlessly create emails with attachment.

Gradle setup

You can always head to for creating a Spring Boot starter project.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'

Creating attachment payload

In this article we will use SendGrid helper class (Attachments) for attaching content of an file with email.

Using Attachments.Builder
package com.sendgrid.helpers.mail.objects;

public class SendGridClient {

    private SendGrid sendGrid;

    void sendCustomEmail() throws IOException {
        Email from = new Email("", "Example Org.");
        String subject = "Sending attachment emails 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);

    void addAttachment(Mail mail) throws IOException {
        try (final InputStream inputStream = Files.newInputStream(Paths.get("/path/to/foo.pdf"))) {
            final Attachments attachments = new Attachments
                    .Builder("foo.pdf", inputStream)    (1)

     void sendInternal(Mail mail) throws IOException {
        Request request = new Request();
        try {
            Response response = sendGrid.api(request);
            if (response.getStatusCode() >= 200 && response.getStatusCode() <= 299) {
                 System.out.println("Successfully sent email with status code: " + response.getStatusCode());
        } catch (IOException ex) {
             System.out.println("Error sending email" + ex.getMessage());
            throw ex;
1 SendGrid does not close the stream, so we must take care of closing it by our own. try-with-resource will take care of this in our case.

Kotlin equivalent is:

Mail Attachment
fun addAttachment(mail: Mail, filePath: String = "/path/to/foo.pdf", name: String = "foo.pdf", contentType: String = "application/pdf") {
    Files.newInputStream(Paths.get(filePath)).use {
        val attachments: Attachments = Attachments.Builder(name, it)

Optionally, we can develop our own function for converting a file attachment to base64 format using standard java library, as shown below:

  • read file into byte array

  • convert bytes to base64 format using Java 8 java.util.Base64

ConvertFileToBase64 - converts byte array to base64
import java.util;
import java.nio.file.Files;

public class Utility {

    private String convertFileToBase64(Path pathToFile) {
      int read = 0;
      byte[] bytes = new byte[4096];
      try(InputStream content = Files.newInputStream(pathToFile)) {
        while ((read = != -1) {
          baos.write(bytes, 0, read);
        return Base64.getEncoder().encodeToString(baos.toByteArray());
      } catch (IOException e) {
        throw new RuntimeException("Unable to convert content stream to base 64 encoded string", e);

On unix systems, you can optionally use command line utility to convert a file into base64 and vice versa.

Encode a string into Base64
$ echo  '' | base64
Decode Base64 content back to String
$ echo 'bGludXhoaW50LmNvbQo=' | base64 --decode
Encode a file to Base64
base64 input.txt > base64Data.txt
Decode Base64 encoded file to original
base64 -d base64Data.txt > original.txt

Error handling

SendGrid may return an error code when Attachment is too large, which is 413 status code. You can learn more about status codes returned by SendGrid API at this link: SendGrid Mail Error Codes

Top articles in this category:
  1. SendGrid emails in Spring Boot
  2. Sendgrid Dynamic Templates with Spring Boot
  3. Basic Auth Security in Spring Boot 2
  4. Running Spring Boot app as a service in unix
  5. Spring Boot 2.0 Reactive Web Performance Metrics
  6. Setting a Random Port in Spring Boot Application at startup
  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