Socket
Socket
Sign inDemoInstall

com.sendgrid:sendgrid-java

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

com.sendgrid:sendgrid-java

This Java module allows you to quickly and easily send emails through Twilio SendGrid using Java.


Version published
Maintainers
1
Source

SendGrid Logo

BuildStatus Maven Central Twitter Follow GitHub contributors Open Source Helpers MIT licensed

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.

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

Table of Contents

Installation

Prerequisites

  • Java 8 or 11
  • The Twilio SendGrid service, starting at the free level to send up to 40,000 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:5.0.0-rc.1'
}

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

Alerts API

Sendgrid provides an API for sending alerts. The following is the minimum needed code to send an alert using the Java Helper Library:

Create Alerts

import com.sendgrid.*;
import java.io.IOException;

public class Example {
  public static void main(String[] args) {
      ApiKeySendGrid.init(System.getenv("SENDGRID_API_KEY"));
      CreateAlertRequest createAlertRequest = new CreateAlertRequest.Builder(Type.USAGE_LIMIT, "example@example.com").percentage(50).build();
    try {
        CreateAlert createAlert =  new CreateAlert();
        createAlert.setCreateAlertRequest(createAlertRequest);
        ApiResponse apiResponse = createAlert.send();
        System.out.println(response.getStatusCode());
        System.out.println(response.getBody());
        System.out.println(response.getHeaders());
    } catch (ApiErrorResponse apiErrorResponse)  {
        System.out.println(apiErrorResponse.getMessage());
    }
  }
}

Fetch a specific alert

import com.sendgrid.*;
import java.io.IOException;

public class Example {
  public static void main(String[] args) {
      ApiKeySendGrid.init(System.getenv("SENDGRID_API_KEY"));
      GetAlert getAlert = new GetAlert(xxxxxxx); // alert id
      ApiResponse getAlertResponse = null;
    try {
        getAlertResponse = getAlert.send();
        GetAlert200Response alert = (GetAlert200Response) getAlertResponse.getBody();
        System.out.println(alert);
    } catch (ApiErrorResponse apiErrorResponse)  {
        System.out.println(apiErrorResponse.getMessage());
    }
  }
}

List all alerts

import com.sendgrid.*;
import java.io.IOException;

public class Example {
  public static void main(String[] args) {
      ApiKeySendGrid.init(System.getenv("SENDGRID_API_KEY"));
      ListAlert listAlert = new ListAlert();
      ApiResponse getAlert = null;
    try {
        getAlert = listAlert.send();
        List<ListAlert200ResponseInner> body = (List<ListAlert200ResponseInner>)getAlert.getBody();
        System.out.println(body);
    } catch (ApiErrorResponse apiErrorResponse)  {
        System.out.println(apiErrorResponse.getMessage());
    }
  }
}

Delete a specific alert

import com.sendgrid.*;
import java.io.IOException;

public class Example {
    public static void main(String[] args) {
        ApiKeySendGrid.init(System.getenv("SENDGRID_API_KEY"));
        DeleteAlert deleteAlert = new DeleteAlert(xxxxxxx); // alert id
        ApiResponse deleteAlertResponse;
        try {
            deleteAlertResponse = deleteAlert.send();
            System.out.println(deleteAlertResponse.getStatusCode());
            System.out.println(deleteAlertResponse.getBody());
            System.out.println(deleteAlertResponse.getHeaders());
            
        } catch (ApiErrorResponse apiErrorResponse)  {
            System.out.println(apiErrorResponse.getMessage());
        }
    }
}

Hello Email

You can send an email with the /mail/send Helper (here is a full example).

With Mail Helper Class

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

Without Mail Helper Class

You can also send an email without the /mail/send Helper (here is a full example).

General v3 Web API Usage

Here is the full example of using v3 web API.

Usage

Use Cases

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

Announcements

All updates to this library are documented in our CHANGELOG and releases.

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.

Support

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

License

The MIT License (MIT)

FAQs

Package last updated on 27 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc