NEW: Subscribe to email notifications for releases and breaking changes.
This library allows you to quickly and easily use the SendGrid Web API v3 via Java.
Version 3.X.X of this library provides full support for all SendGrid Web API v3 endpoints, including the new v3 /mail/send.
This library represents the beginning of a new path for SendGrid. We want this library to be community driven and SendGrid led. We need your help to realize this goal. To help make sure we are building the right things in the right order, we ask that you create issues and pull requests or simply upvote or comment on existing issues or pull requests.
Please browse the rest of this README for further detail.
We appreciate your continued support, thank you!
- Installation
- Quick Start
- Usage
- Use Cases
- Announcements
- Roadmap
- How to Contribute
- Troubleshooting
- About
- Java version Oracle JDK 7, 8 or OpenJDK 7
- The SendGrid service, starting at the free level
Update the development environment with your SENDGRID_API_KEY, for example:
echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env
Choose your installation method - Maven w/ Gradle (recommended), Maven or Jar file.
Add the following to your build.gradle file in the root of your project.
...
dependencies {
...
compile 'com.sendgrid:sendgrid-java:4.1.0'
}
repositories {
mavenCentral()
}
...
mvn install
You can just drop the jar file in. It's a fat jar - it has all the dependencies built in.
The following is the minimum needed code to send an email with the /mail/send Helper (here is a full example):
import com.sendgrid.*;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException {
Email from = new Email("[email protected]");
String subject = "Sending with SendGrid is Fun";
Email to = new Email("[email protected]");
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;
}
}
}
The Mail
constructor creates a personalization object for you. Here is an example of how to add to it.
The following is the minimum needed code to send an email without the /mail/send Helper (here is a full example):
import com.sendgrid.*;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException {
try {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody("{\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Sending with SendGrid is Fun\"}],\"from\":{\"email\":\"[email protected]\"},\"content\":[{\"type\":\"text/plain\",\"value\": \"and easy to do anywhere, even with Java\"}]}");
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;
}
}
}
import com.sendgrid.*;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
try {
Request request = new Request();
request.setMethod(Method.GET);
request.setEndpoint("api_keys");
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;
}
}
}
- SendGrid Docs
- Library Usage Docs
- Example Code
- How-to: Migration from v2 to v3
- v3 Web API Mail Send Helper - build a request object payload for a v3 /mail/send API call.
Examples of common API use cases, such as how to send an email with a transactional template.
Please see our announcement regarding breaking changes. Your support is appreciated!
All updates to this library are documented in our CHANGELOG and releases. You may also subscribe to email release notifications for releases and breaking changes.
If you are interested in the future direction of this project, please take a look at our open issues and pull requests. We would love to hear your feedback.
We encourage contribution to our libraries (you might even score some nifty swag), please see our CONTRIBUTING guide for details.
Quick links:
Please see our troubleshooting guide for common library issues.
sendgrid-java is guided and supported by the SendGrid Developer Experience Team.
sendgrid-java is maintained and funded by SendGrid, Inc. The names and logos for sendgrid-java are trademarks of SendGrid, Inc.
![SendGrid Logo] (https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)