Skip to content

nquinlan/sendgrid-java

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SendGrid-Java

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

import com.sendgrid.*;
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");

Mail mail = new Mail();
mail.addTo("example@example.com");
mail.setFrom("other@example.com");
mail.setSubject("Hello World");
mail.setText("My first email through SendGrid");

sendgrid.send(mail);

Installation

There are multiple ways to install this library. I recommend using Maven w/ Gradle.

via Maven w/ Gradle

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

...
dependencies {
  ...
  compile 'com.sendgrid:sendgrid-java:0.1.2'
}

repositories {
  mavenCentral()
}
...

Then import the library - in the file appropriate to your Java project.

import com.sendgrid.SendGrid;

via jar file

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

sendgrid-0.1.2-jar.jar

import com.sendgrid.*;

Usage

To begin using this library, initialize the SendGrid object with your SendGrid credentials.

import com.sendgrid.SendGrid;
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");

Add your message details.

Mail mail = new Mail();
mail.addTo("example@example.com");
mail.addToName("Example Guy");
mail.setFrom("other@example.com");
mail.setSubject("Hello World");
mail.setText("My first email through SendGrid");

Send it.

sendgrid.send(mail);

To

mail.addTo("example@example.com");
// or
mail.addTos(["other@other.com"]);
// or
mail.setTos(["other@other.com"]);

To Name

mail.addToName("Example Guy");
// or
mail.addToNames(["Other Gal"]);
//or
mail.setToNames(["Other Gal"]);

Bcc

mail.addBcc("yourself@yourself.com");
// or
mail.addBccs(["yourself@yourself.com"]);
// or
mail.setBccs([]"yourself@yourself.com"]);

From

mail.setFrom("other@example.com");

From Name

mail.setFromName("Other Dude");

Reply To

mail.setReplyTo("no-reply@nowhere.com");

Subject

mail.setSubject("Hello World");

Text

mail.setText("This is some text of the email.");

Html

mail.setHtml("<h1>My first email through SendGrid");

Attachments

mail.addAttachment("contents", "text.txt");
// or
mail.addAttachment(new File("./file.txt"), "text.txt");
// or
mail.addAttachment(new InputStream(new File("./file.txt")), "text.txt");

The mail object extends de SMTPAPI object which is found in STMAPI-Java.

header.addSubstitution("key", "value");
// or
header.setSubstitutions("key", ["value1", "value2"]);

JSONObject subs = header.getSubstitutions();
header.addUuniqueAarg("key", "value");
// or
Map map = new HashMap<String, String>();
map.put("unique", "value");
header.setUniqueArgs(map);
// or
JSONObject map = new JSONObject();
map.put("unique", "value");
header.setUniqueArgs(map);

JSONObject args = header.getUniqueArgs();
header.addCategory("category");
// or
header.addCategory(["categories"]);
// or
header.setCategories(["category1", "category2"]);

String[] cats = header.getCategories();
header.addSection("key", "section");
// or
Map newSec = new HashMap();
newSec.put("-section-", "value");
header.setSections(newSec);
// or
JSONObject newSec = new JSONObject();
newSec.put("-section-", "value");
header.setSections(newSec);

JSONObject sections = header.getSections();
header.addFilter("filter", "setting", "value");
header.addFilter("filter", "setting", 1);

JSONObject filters = header.getFilters();

Get Headers

String headers = header.jsonString();

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Running Tests

The existing tests in the src/test directory can be run using gradle with the following command:

gradle build

Generating the jar

gradle build

(If you don't have gradle install it. If on a mac, you can run brew install gradle)

License

Licensed under the MIT License.

About

SendGrid Java helper library

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 68.0%
  • Groovy 18.8%
  • PHP 8.6%
  • Shell 4.6%