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);There are multiple ways to install this library. I recommend using 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;You can just drop the jar file in. It's a fat jar - it has all the dependencies built in.
import com.sendgrid.*;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);mail.addTo("example@example.com");
// or
mail.addTos(["other@other.com"]);
// or
mail.setTos(["other@other.com"]);mail.addToName("Example Guy");
// or
mail.addToNames(["Other Gal"]);
//or
mail.setToNames(["Other Gal"]);mail.addBcc("yourself@yourself.com");
// or
mail.addBccs(["yourself@yourself.com"]);
// or
mail.setBccs([]"yourself@yourself.com"]);mail.setFrom("other@example.com");mail.setFromName("Other Dude");mail.setReplyTo("no-reply@nowhere.com");mail.setSubject("Hello World");mail.setText("This is some text of the email.");mail.setHtml("<h1>My first email through SendGrid");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();String headers = header.jsonString();- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Added some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
The existing tests in the src/test directory can be run using gradle with the following command:
gradle buildgradle build(If you don't have gradle install it. If on a mac, you can run brew install gradle)
Licensed under the MIT License.