sendgrid/sendgrid-java


The Official Twilio SendGrid Led, Community Driven Java API Library

https://sendgrid.com

License: MIT

Language: Java

Keywords: email, java, sendgrid, transactional-emails


SendGrid Logo

Travis Badge Maven Central Email Notifications Badge Twitter Follow GitHub contributors Open Source Helpers MIT licensed

NEW: Subscribe to email notifications for releases and breaking changes.

This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via Java.

Version 3.X.X of this library provides full support for all Twilio SendGrid Web API v3 endpoints, including the new v3 /mail/send.

This library represents the beginning of a new path for Twilio SendGrid. We want this library to be community driven and Twilio 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 details.

We appreciate your continued support, thank you!

Table of Contents

Installation

Prerequisites

  • Java version Oracle JDK 7, 8 or OpenJDK 7
  • The Twilio SendGrid service, starting at the free level to send up to 4.5.0 emails for the first 30 days, then send 100 emails/day free forever or check out our pricing.

Setup Environment Variables

Update the development environment with your SENDGRID_API_KEY, for example:

  1. Copy the sample environment file to a new file
cp .env_sample .env
  1. Edit the new .env to add your API key
  2. Source the .env file to set the variable in the current session
source .env

Install Package

Choose your installation method - Maven w/ Gradle (recommended), Maven or Jar file.

via Maven w/ Gradle

Add the following to your build.gradle file in the root of your project.

...
dependencies {
  ...
  implementation 'com.sendgrid:sendgrid-java:4.5.0'
}

repositories {
  mavenCentral()
}
...

via Maven

mvn install

via jar file

You can just drop the jar file in. It's a fat jar - it has all the dependencies built in.

sendgrid-java.jar

Dependencies

Quick Start

Hello Email

The following is the minimum needed code to send an email with the /mail/send Helper (here is a full example):

With Mail Helper Class

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 Twilio 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;
    }
  }
}

The Mail constructor creates a personalization object for you. Here is an example of how to add to it.

Without Mail Helper Class

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\":\"test@example.com\"}],\"subject\":\"Sending with Twilio SendGrid is Fun\"}],\"from\":{\"email\":\"test@example.com\"},\"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;
    }
  }
}

General v3 Web API Usage

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;
    }
  }
}

Usage

Use Cases

Examples of common API use cases, such as how to send an email with a transactional template.

Announcements

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.

Roadmap

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.

How to Contribute

We encourage contribution to our libraries (you might even score some nifty swag), please see our CONTRIBUTING guide for details.

Quick links:

Troubleshooting

Please see our troubleshooting guide for common library issues.

About

sendgrid-java is maintained and funded by Twilio SendGrid, Inc. The names and logos for sendgrid-java are trademarks of Twilio SendGrid, Inc.

If you need help installing or using the library, please check the Twilio SendGrid Support Help Center.

If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!

License

The MIT License (MIT)

Project Statistics

Sourcerank 13
Repository Size 4.3 MB
Stars 338
Forks 328
Watchers 211
Open issues 142
Dependencies 7
Contributors 82
Tags 51
Created
Last updated
Last pushed

Top Contributors See all

Elmer Thomas Mot Eddie Zaneski Dmitry Avershin twilio-ci childish-sambino sccalabr Alex Andrew Trimble Marcus Vinícius Elise Shanholtz John Claus Matt Bernier Anshul Singhal Diego Camargo Chandler Weiner Yamil Asusta Shashank Sharma Cícero Pablo Brandon West

Packages Referencing this Repo

com.sendgrid:sendgrid-java
This Java module allows you to quickly and easily send emails through Twilio SendGrid using Java.
Latest release 4.5.0 - Updated - 338 stars

Recent Tags See all

4.5.0 May 13, 2020
4.4.8 April 15, 2020
4.4.7 April 01, 2020
4.4.6 March 18, 2020
4.4.5 February 20, 2020
4.4.4 February 08, 2020
4.4.3 February 05, 2020
4.4.2 February 04, 2020
v4.4.1 May 20, 2019
v4.3.0 October 11, 2018
v3.2.2 May 08, 2018
v3.2.2 May 08, 2018
v4.2.1 May 08, 2018
v4.2.0 May 04, 2018
v4.1.2 October 31, 2017

Interesting Forks See all

mongolab/sendgrid-java
SendGrid Java helper library
Updated - 1 stars

Something wrong with this page? Make a suggestion

Last synced: 2020-05-21 17:31:26 UTC

Login to resync this repository