Skip to content

Commit f6f4512

Browse files
jreyesmotdotla
authored andcommitted
Added attachment support
1 parent 15f59fb commit f6f4512

File tree

5 files changed

+196
-83
lines changed

5 files changed

+196
-83
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
build
22
.gradle
3+
.idea
4+
*.iml
5+
out

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ buildscript {
1313

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

1819
repositories {

src/main/java/com/github/scottmotte/sendgrid/Sendgrid.java

Lines changed: 111 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,116 @@
22

33
import com.github.kevinsawicki.http.HttpRequest;
44

5-
public class Sendgrid {
6-
protected String username;
7-
protected String password;
8-
9-
private String to = "";
10-
private String from = "";
11-
private String subject = "";
12-
private String text = "";
13-
private String html = "";
14-
15-
public Sendgrid(String username, String password) {
16-
this.username = username;
17-
this.password = password;
18-
}
19-
20-
public void send() {
21-
this.web();
22-
}
23-
24-
public void web() {
25-
HttpRequest request = HttpRequest.post("https://sendgrid.com/api/mail.send.json");
26-
27-
request.part("api_user", this.username);
28-
request.part("api_key", this.password);
29-
request.part("to", this.getTo());
30-
request.part("subject", this.getSubject());
31-
request.part("from", this.getFrom());
32-
request.part("text", this.getText());
33-
request.part("html", this.getHtml());
34-
35-
String response = request.body();
36-
37-
System.out.println(response);
38-
}
39-
40-
public String getTo() {
41-
return this.to;
42-
}
43-
44-
public String getFrom() {
45-
return this.from;
46-
}
47-
48-
public String getSubject() {
49-
return this.subject;
50-
}
51-
52-
public String getText() {
53-
return this.text;
54-
}
5+
import java.io.File;
556

56-
public String getHtml() {
57-
return this.html;
58-
}
59-
60-
public Sendgrid setTo(String email) {
61-
this.to = email;
62-
63-
return this;
64-
}
65-
66-
public Sendgrid setFrom(String email) {
67-
this.from = email;
68-
69-
return this;
70-
}
71-
72-
public Sendgrid setSubject(String subject) {
73-
this.subject = subject;
74-
75-
return this;
76-
}
77-
78-
public Sendgrid setText(String text) {
79-
this.text = text;
80-
81-
return this;
82-
}
83-
84-
public Sendgrid setHtml(String html) {
85-
this.html = html;
86-
87-
return this;
88-
}
7+
public class Sendgrid {
8+
// ------------------------------ FIELDS ------------------------------
9+
10+
private static final String PARAM_API_KEY = "api_key";
11+
private static final String PARAM_API_USER = "api_user";
12+
private static final String PARAM_BCC = "bcc";
13+
private static final String PARAM_FILES = "files[%s]";
14+
private static final String PARAM_FROM = "from";
15+
private static final String PARAM_HTML = "html";
16+
private static final String PARAM_SUBJECT = "subject";
17+
private static final String PARAM_TEXT = "text";
18+
private static final String PARAM_TO = "to";
19+
20+
private String[] bcc;
21+
private File[] file;
22+
private String from;
23+
private String html;
24+
private String password;
25+
private String subject;
26+
private String text;
27+
private String[] to;
28+
private String username;
29+
30+
// -------------------------- STATIC METHODS --------------------------
31+
32+
public static Sendgrid withCredentials(String username, String password) {
33+
return new Sendgrid(username, password);
34+
}
35+
36+
// --------------------------- CONSTRUCTORS ---------------------------
37+
38+
public Sendgrid(final String username, final String password) {
39+
this.username = username;
40+
this.password = password;
41+
}
42+
43+
// -------------------------- OTHER METHODS --------------------------
44+
45+
public Sendgrid bcc(final String... bcc) {
46+
this.bcc = bcc;
47+
return this;
48+
}
49+
50+
public Sendgrid from(final String email) {
51+
this.from = email;
52+
return this;
53+
}
54+
55+
public String send() {
56+
HttpRequest request = HttpRequest.post("https://sendgrid.com/api/mail.send.json");
57+
if (username != null) {
58+
request.part(PARAM_API_USER, username);
59+
}
60+
if (password != null) {
61+
request.part(PARAM_API_KEY, password);
62+
}
63+
if (from != null) {
64+
request.part(PARAM_FROM, from);
65+
}
66+
if (to != null) {
67+
for (String s : to) {
68+
request.part(PARAM_TO, s);
69+
}
70+
}
71+
if (bcc != null) {
72+
for (String s : bcc) {
73+
request.part(PARAM_BCC, s);
74+
}
75+
}
76+
if (subject != null) {
77+
request.part(PARAM_SUBJECT, subject);
78+
}
79+
if (text != null) {
80+
request.part(PARAM_TEXT, text);
81+
}
82+
if (html != null) {
83+
request.part(PARAM_HTML, html);
84+
}
85+
if (file != null) {
86+
for (File f : file) {
87+
request.part(String.format(PARAM_FILES, f.getName()), f);
88+
}
89+
}
90+
return request.body();
91+
}
92+
93+
public Sendgrid to(final String... to) {
94+
this.to = to;
95+
return this;
96+
}
97+
98+
public Sendgrid withAttachment(final File... file) {
99+
this.file = file;
100+
return this;
101+
}
102+
103+
public Sendgrid withHtml(final String html) {
104+
this.html = html;
105+
return this;
106+
}
107+
108+
public Sendgrid withSubject(final String subject) {
109+
this.subject = subject;
110+
return this;
111+
}
112+
113+
public Sendgrid withText(final String text) {
114+
this.text = text;
115+
return this;
116+
}
89117
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,81 @@
11
package com.github.scottmotte.sendgrid;
2+
3+
import org.junit.Test;
4+
5+
import java.io.File;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class SendgridTest {
10+
// ------------------------------ FIELDS ------------------------------
11+
12+
private static final String FROM_EMAIL = "[email protected]";
13+
private static final String PASSWORD = "password";
14+
private static final String TO_ANOTHER_EMAIL = "[email protected]";
15+
private static final String TO_EMAIL = "[email protected]";
16+
private static final String USERNAME = "username";
17+
18+
// -------------------------- OTHER METHODS --------------------------
19+
20+
@Test
21+
public void testSendSuccess() {
22+
String result = Sendgrid
23+
.withCredentials(USERNAME, PASSWORD)
24+
.from(FROM_EMAIL)
25+
.to(TO_EMAIL)
26+
.withSubject("This is a test subject")
27+
.withText("This is a test text.")
28+
.send();
29+
assertEquals("{\"message\":\"success\"}", result);
30+
}
31+
32+
@Test
33+
public void testSendSuccessBcc() {
34+
String result = Sendgrid
35+
.withCredentials(USERNAME, PASSWORD)
36+
.from(FROM_EMAIL)
37+
.to(TO_EMAIL)
38+
.bcc(TO_ANOTHER_EMAIL)
39+
.withSubject("This is a test subject")
40+
.withText("This is a test text.")
41+
.send();
42+
assertEquals("{\"message\":\"success\"}", result);
43+
}
44+
45+
@Test
46+
public void testSendSuccessWithAttachment() {
47+
File attachment = new File(getClass().getResource("/test.txt").getFile());
48+
String result = Sendgrid
49+
.withCredentials(USERNAME, PASSWORD)
50+
.from(FROM_EMAIL)
51+
.to(TO_EMAIL)
52+
.withSubject("This is a test subject")
53+
.withText("This is a test text.")
54+
.withAttachment(attachment)
55+
.send();
56+
assertEquals("{\"message\":\"success\"}", result);
57+
}
58+
59+
@Test
60+
public void testSendSuccessWithMultipleRecipients() {
61+
String result = Sendgrid
62+
.withCredentials(USERNAME, PASSWORD)
63+
.from(FROM_EMAIL)
64+
.to(TO_EMAIL, TO_ANOTHER_EMAIL)
65+
.withSubject("This is a test subject")
66+
.withText("This is a test text.")
67+
.send();
68+
assertEquals("{\"message\":\"success\"}", result);
69+
}
70+
71+
@Test
72+
public void testSendWithoutFrom() {
73+
String result = Sendgrid
74+
.withCredentials(USERNAME, PASSWORD)
75+
.to(TO_EMAIL)
76+
.withSubject("This is a test subject")
77+
.withText("This is a test text.")
78+
.send();
79+
assertEquals("{\"message\": \"error\", \"errors\": [\"Empty from email address (required)\"]}", result);
80+
}
81+
}

src/test/resources/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a test file.

0 commit comments

Comments
 (0)