implementation 'com.sendgrid:sendgrid-java:4.6.1'
SendGrid emails in Spring Boot
Upasana | 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
.
You can find the latest version of sendgrid-java
from https://github.com/sendgrid/sendgrid-java/releases
# 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.
pring.sendgrid.proxy.host: # 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;
import java.io.IOException;
@Component
public class SendGridMailer {
@Autowired
private SendGridAPI sendGridAPI;
void sendMail() {
Email from = new Email("from@example.com", "Foo Bar");
String subject = "Test email with SendGrid";
Email to = new Email("to@example.com");
Content content = new Content("text/plain", "Test email with Spring");
Mail mail = new Mail(from, subject, to, content);
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sendGridAPI.api(request);
System.out.println(response.getBody());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
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.
// using SendGrid's Java Library
// https://github.com/sendgrid/sendgrid-java
import com.sendgrid.*;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException {
Email from = new Email("test@example.com");
String subject = "Sending with SendGrid is Fun";
Email to = new Email("test@example.com");
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 {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
}
}
Did you know?
You can use Unix command line utility to encode and decode in base64 format.
$ echo 'linuxhint.com' | base64
$ echo 'bGludXhoaW50LmNvbQo=' | base64 --decode
That’s all.
Top articles in this category:
- Sendgrid Dynamic Templates with Spring Boot
- SendGrid Attachments with Spring Boot
- Redis rate limiter in Spring Boot
- Setting a Random Port in Spring Boot Application at startup
- Basic Auth Security in Spring Boot 2
- Testing web layer in Spring Boot using WebMvcTest
- Feign RequestInterceptor in Spring Boot