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("[email protected]");
sendgrid.setFrom("[email protected]");
sendgrid.setSubject("Hello World");
sendgrid.setText("My first email through SendGrid");
sendgrid.send();There are multiple ways to install this library. I recommend using Gradle.
Add the following to your build.gradle file in the root of your project.
...
repositories {
mavenCentral()
add(new org.apache.ivy.plugins.resolver.URLResolver()) {
name = 'GitHub'
addArtifactPattern 'https://github.com/sendgrid/sendgrid-java/raw/v[revision]/repo/com/github/sendgrid/[revision]/sendgrid-[revision]-jar.jar'
}
}
dependencies {
...
compile 'com.github.sendgrid:sendgrid:0.1.0'
}
...Then import the library - in the file appropriate to your Java project.
import com.github.sendgrid.SendGrid;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 will also need to include the http-request library from kevinsawicki.
I'd like to get this on Maven. Please create an issue if you'd like to see it on Maven as well.
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("[email protected]");
sendgrid.addToName("Example Guy");
sendgrid.setFrom("[email protected]");
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("[email protected]");
sendgrid.addTo("[email protected]");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("[email protected]");
sendgrid.addToName("Example Guy");
sendgrid.addTo("[email protected]");
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("[email protected]");
...
sendgrid.setFrom("[email protected]");SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("[email protected]");
...
sendgrid.setFrom("[email protected]");
sendgrid.setFromName("Other Dude");SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("[email protected]");
...
sendgrid.setReplyTo("[email protected]");SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("[email protected]");
...
sendgrid.setSubject("Hello World");SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("[email protected]");
...
sendgrid.setText("This is some text of the email.");SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("[email protected]");
...
sendgrid.setHtml(<h1>My first email through SendGrid");import java.io.File;
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("[email protected]");
...
sendgrid.addFile(new File("../path/to/file.txt");Use multiple addTos as a superior alternative to setBcc.
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("[email protected]");
sendgrid.addTo("[email protected]");
sendgrid.addTo("[email protected]");
...If you still absolutely need to use Bcc, you can use sendgrid.addBcc("[email protected]");
Headers can be used to add existing SendGrid functionality (like categories or filters through the smtpapi header) or your own custom headers.
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("[email protected]");
...
sendgrid.addHeader("category", "My New Category");- 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)
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.