This Java module allows you to quickly and easily send emails through SendGrid using Java.
import com.github.sendgrid.SendGrid;
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
sendgrid.setFrom("other@example.com");
sendgrid.setSubject("Hello World");
sendgrid.setText("My first email through SendGrid");
sendgrid.send();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.2.0'
}
repositories {
mavenCentral()
}
...Then import the library - in the file appropriate to your Java project.
import com.github.sendgrid.SendGrid;You can just drop the jar file in. It's a fat jar - it has all the dependencies built in.
Copy and paste the SendGrid.java file into your project. That file is available here: https://github.com/sendgrid/sendgrid-java/blob/master/src/main/java/com/github/sendgrid/SendGrid.java
Then import the library - in the file appropriate to your Java project.
import com.github.sendgrid.SendGrid;- You need to include the http-request library from kevinsawicki.
- You need to include the json library from chargebee
There is a sendgrid-java-example app to help jumpstart your development.
To begin using this library, initialize the SendGrid object with your SendGrid credentials.
import com.github.sendgrid.SendGrid;
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");Add your message details.
sendgrid.addTo("example@example.com");
sendgrid.addToName("Example Guy");
sendgrid.setFrom("other@example.com");
sendgrid.setSubject("Hello World");
sendgrid.setText("My first email through SendGrid");Send it.
sendgrid.send();SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
sendgrid.addTo("other@other.com");You can add multiple tos as necessary. She will get the email as if it was sent solely to her.
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
sendgrid.addToName("Example Guy");
sendgrid.addTo("other@other.com");
sendgrid.addToName("Other Gal");You can add multiple tonames as necessary. They should be set in the same array order as the emails.
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
sendgrid.setFrom("other@example.com");SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
sendgrid.setFrom("other@example.com");
sendgrid.setFromName("Other Dude");SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
sendgrid.setReplyTo("no-reply@nowhere.com");SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
sendgrid.setSubject("Hello World");SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
sendgrid.setText("This is some text of the email.");SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
sendgrid.setHtml(<h1>My first email through SendGrid");import java.io.File;
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
sendgrid.addFile(new File("../path/to/file.txt"));If you need to add files from an InputStream (maybe you're on Google App Engine and have the contents in a byte array), here is how.
import java.io.ByteArrayInputStream;
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
byte[] contents = somehowGenerateAttachmentContents();
SendGrid.Attachment attachment1 = new SendGrid.Attachment("filename.txt", new ByteArrayInputStream(contents));
sendgrid.addFile(attachment1);
sendgrid.send();Use multiple addTos as a superior alternative to setBcc.
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
sendgrid.addTo("other@other.com");
sendgrid.addTo("yourself@yourself.com");
...If you still absolutely need to use Bcc, you can use sendgrid.addBcc("email@somewhere.com");
Add up to 10 categories to the object.
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
sendgrid.addCategory("sample_email");
sendgrid.addCategory("dev_test");You can add custom headers.
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
sendgrid.addHeader("X-Sent-Using", "SendGrid-API");
sendgrid.addHeader("X-Transport", "web");To add SendGrid style headers for things such as categories or filters, do the following.
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
sendgrid.addHeader("X-SMTPAPI", "{\"category\":\"My New Category\"}");You can enable and configure Apps.
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
...
sendgrid.addFilter("bcc", "enable", 1);
sendgrid.addFilter("bcc", "email", "example@example.com");- 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)
This only works if you have the correct permissions - for admins only basically.
gradle uploadArchives
Login to Sonatype.
Go to staging repositories page.
Click 'Close' with the archive selected.
Wait a few minutes, and refresh the staging repositories page.
Check the box for the SendGrid repo again and this time click 'Release'.
You're all done.
We have an example app using this library. This can be helpful to get a grasp on implementing it in your own app. The example below is a spring based application.
github.com/scottmotte/spring-attack
Licensed under the MIT License.
