Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
build
.gradle
.idea
*.iml
out
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ buildscript {

dependencies {
compile 'com.github.kevinsawicki:http-request:5.4.1'
testCompile group: 'junit', name: 'junit', version: '4.+'
}

repositories {
Expand Down Expand Up @@ -82,7 +83,7 @@ uploadArchives {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
authentication(userName: sonatypeUsername, password: sonatypePassword)
//authentication(userName: sonatypeUsername, password: sonatypePassword)
}

pom.project {
Expand Down
194 changes: 111 additions & 83 deletions src/main/java/com/github/scottmotte/sendgrid/Sendgrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,116 @@

import com.github.kevinsawicki.http.HttpRequest;

public class Sendgrid {
protected String username;
protected String password;

private String to = "";
private String from = "";
private String subject = "";
private String text = "";
private String html = "";

public Sendgrid(String username, String password) {
this.username = username;
this.password = password;
}

public void send() {
this.web();
}

public void web() {
HttpRequest request = HttpRequest.post("https://sendgrid.com/api/mail.send.json");

request.part("api_user", this.username);
request.part("api_key", this.password);
request.part("to", this.getTo());
request.part("subject", this.getSubject());
request.part("from", this.getFrom());
request.part("text", this.getText());
request.part("html", this.getHtml());

String response = request.body();

System.out.println(response);
}

public String getTo() {
return this.to;
}

public String getFrom() {
return this.from;
}

public String getSubject() {
return this.subject;
}

public String getText() {
return this.text;
}
import java.io.File;

public String getHtml() {
return this.html;
}

public Sendgrid setTo(String email) {
this.to = email;

return this;
}

public Sendgrid setFrom(String email) {
this.from = email;

return this;
}

public Sendgrid setSubject(String subject) {
this.subject = subject;

return this;
}

public Sendgrid setText(String text) {
this.text = text;

return this;
}

public Sendgrid setHtml(String html) {
this.html = html;

return this;
}
public class Sendgrid {
// ------------------------------ FIELDS ------------------------------

private static final String PARAM_API_KEY = "api_key";
private static final String PARAM_API_USER = "api_user";
private static final String PARAM_BCC = "bcc";
private static final String PARAM_FILES = "files[%s]";
private static final String PARAM_FROM = "from";
private static final String PARAM_HTML = "html";
private static final String PARAM_SUBJECT = "subject";
private static final String PARAM_TEXT = "text";
private static final String PARAM_TO = "to";

private String[] bcc;
private File[] file;
private String from;
private String html;
private String password;
private String subject;
private String text;
private String[] to;
private String username;

// -------------------------- STATIC METHODS --------------------------

public static Sendgrid withCredentials(String username, String password) {
return new Sendgrid(username, password);
}

// --------------------------- CONSTRUCTORS ---------------------------

public Sendgrid(final String username, final String password) {
this.username = username;
this.password = password;
}

// -------------------------- OTHER METHODS --------------------------

public Sendgrid bcc(final String... bcc) {
this.bcc = bcc;
return this;
}

public Sendgrid from(final String email) {
this.from = email;
return this;
}

public String send() {
HttpRequest request = HttpRequest.post("https://sendgrid.com/api/mail.send.json");
if (username != null) {
request.part(PARAM_API_USER, username);
}
if (password != null) {
request.part(PARAM_API_KEY, password);
}
if (from != null) {
request.part(PARAM_FROM, from);
}
if (to != null) {
for (String s : to) {
request.part(PARAM_TO, s);
}
}
if (bcc != null) {
for (String s : bcc) {
request.part(PARAM_BCC, s);
}
}
if (subject != null) {
request.part(PARAM_SUBJECT, subject);
}
if (text != null) {
request.part(PARAM_TEXT, text);
}
if (html != null) {
request.part(PARAM_HTML, html);
}
if (file != null) {
for (File f : file) {
request.part(String.format(PARAM_FILES, f.getName()), f);
}
}
return request.body();
}

public Sendgrid to(final String... to) {
this.to = to;
return this;
}

public Sendgrid withAttachment(final File... file) {
this.file = file;
return this;
}

public Sendgrid withHtml(final String html) {
this.html = html;
return this;
}

public Sendgrid withSubject(final String subject) {
this.subject = subject;
return this;
}

public Sendgrid withText(final String text) {
this.text = text;
return this;
}
}
80 changes: 80 additions & 0 deletions src/test/java/com/github/scottmotte/sendgrid/SendgridTest.java
Original file line number Diff line number Diff line change
@@ -1 +1,81 @@
package com.github.scottmotte.sendgrid;

import org.junit.Test;

import java.io.File;

import static org.junit.Assert.assertEquals;

public class SendgridTest {
// ------------------------------ FIELDS ------------------------------

private static final String FROM_EMAIL = "[email protected]";
private static final String PASSWORD = "password";
private static final String TO_ANOTHER_EMAIL = "[email protected]";
private static final String TO_EMAIL = "[email protected]";
private static final String USERNAME = "username";

// -------------------------- OTHER METHODS --------------------------

@Test
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love that there are tests now! thanks @jreyes

public void testSendSuccess() {
String result = Sendgrid
.withCredentials(USERNAME, PASSWORD)
.from(FROM_EMAIL)
.to(TO_EMAIL)
.withSubject("This is a test subject")
.withText("This is a test text.")
.send();
assertEquals("{\"message\":\"success\"}", result);
}

@Test
public void testSendSuccessBcc() {
String result = Sendgrid
.withCredentials(USERNAME, PASSWORD)
.from(FROM_EMAIL)
.to(TO_EMAIL)
.bcc(TO_ANOTHER_EMAIL)
.withSubject("This is a test subject")
.withText("This is a test text.")
.send();
assertEquals("{\"message\":\"success\"}", result);
}

@Test
public void testSendSuccessWithAttachment() {
File attachment = new File(getClass().getResource("/test.txt").getFile());
String result = Sendgrid
.withCredentials(USERNAME, PASSWORD)
.from(FROM_EMAIL)
.to(TO_EMAIL)
.withSubject("This is a test subject")
.withText("This is a test text.")
.withAttachment(attachment)
.send();
assertEquals("{\"message\":\"success\"}", result);
}

@Test
public void testSendSuccessWithMultipleRecipients() {
String result = Sendgrid
.withCredentials(USERNAME, PASSWORD)
.from(FROM_EMAIL)
.to(TO_EMAIL, TO_ANOTHER_EMAIL)
.withSubject("This is a test subject")
.withText("This is a test text.")
.send();
assertEquals("{\"message\":\"success\"}", result);
}

@Test
public void testSendWithoutFrom() {
String result = Sendgrid
.withCredentials(USERNAME, PASSWORD)
.to(TO_EMAIL)
.withSubject("This is a test subject")
.withText("This is a test text.")
.send();
assertEquals("{\"message\": \"error\", \"errors\": [\"Empty from email address (required)\"]}", result);
}
}
1 change: 1 addition & 0 deletions src/test/resources/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test file.