Skip to content

Commit 7dd7f8b

Browse files
author
elbuo8
committed
Added CC Support
1 parent c293f06 commit 7dd7f8b

2 files changed

Lines changed: 49 additions & 11 deletions

File tree

src/main/java/com/sendgrid/SendGrid.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
import org.apache.http.entity.ContentType;
2929

3030
public class SendGrid {
31-
private static final String VERSION = "1.1.0";
31+
private static final String VERSION = "1.2.0";
3232
private static final String USER_AGENT = "sendgrid/" + VERSION + ";java";
3333

3434
private static final String PARAM_TO = "to[%d]";
3535
private static final String PARAM_TONAME = "toname[%d]";
36+
private static final String PARAM_CC = "cc[%d]";
3637
private static final String PARAM_FROM = "from";
3738
private static final String PARAM_FROMNAME = "fromname";
3839
private static final String PARAM_REPLYTO = "replyto";
@@ -84,14 +85,19 @@ public HttpEntity buildBody(Email email) {
8485
builder.addTextBody("api_user", this.username);
8586
builder.addTextBody("api_key", this.password);
8687

87-
for (int i = 0, len = email.getTos().length; i < len; i++)
88-
builder.addTextBody(String.format(PARAM_TO, i), email.getTos()[i]);
89-
90-
for (int i = 0, len = email.getToNames().length; i < len; i++)
91-
builder.addTextBody(String.format(PARAM_TONAME, i), email.getToNames()[i], ContentType.create("text/plain", "UTF-8"));
92-
93-
for (int i = 0, len = email.getBccs().length; i < len; i++)
94-
builder.addTextBody(String.format(PARAM_BCC, i), email.getBccs()[i]);
88+
String[] tos = email.getTos();
89+
String[] tonames = email.getToNames();
90+
String[] ccs = email.getCcs();
91+
String[] bccs = email.getBccs();
92+
93+
for (int i = 0, len = tos.length; i < len; i++)
94+
builder.addTextBody(String.format(PARAM_TO, i), tos[i]);
95+
for (int i = 0, len = tonames.length; i < len; i++)
96+
builder.addTextBody(String.format(PARAM_TONAME, i), tonames[i], ContentType.create("text/plain", "UTF-8"));
97+
for (int i = 0, len = ccs.length; i < len; i++)
98+
builder.addTextBody(String.format(PARAM_CC, i), ccs[i]);
99+
for (int i = 0, len = bccs.length; i < len; i++)
100+
builder.addTextBody(String.format(PARAM_BCC, i), bccs[i]);
95101
// Files
96102
if (email.getAttachments().size() > 0) {
97103
Iterator it = email.getAttachments().entrySet().iterator();
@@ -122,9 +128,9 @@ public HttpEntity buildBody(Email email) {
122128
if (email.getText() != null && !email.getText().isEmpty())
123129
builder.addTextBody(PARAM_TEXT, email.getText(), ContentType.create("text/plain", "UTF-8"));
124130

125-
if (!email.getSMTPAPI().jsonString().equals("{}")) {
131+
if (!email.getSMTPAPI().jsonString().equals("{}"))
126132
builder.addTextBody(PARAM_XSMTPAPI, email.getSMTPAPI().jsonString());
127-
}
133+
128134
return builder.build();
129135
}
130136

@@ -144,6 +150,7 @@ public static class Email {
144150
private SMTPAPI smtpapi;
145151
private ArrayList<String> to;
146152
private ArrayList<String> toname;
153+
private ArrayList<String> cc;
147154
private String from;
148155
private String fromname;
149156
private String replyto;
@@ -158,6 +165,7 @@ public Email () {
158165
this.smtpapi = new SMTPAPI();
159166
this.to = new ArrayList<String>();
160167
this.toname = new ArrayList<String>();
168+
this.cc = new ArrayList<String>();
161169
this.bcc = new ArrayList<String>();
162170
this.attachments = new HashMap<String, InputStream>();
163171
this.headers = new HashMap<String, String>();
@@ -209,6 +217,25 @@ public String[] getToNames() {
209217
return this.toname.toArray(new String[this.toname.size()]);
210218
}
211219

220+
public Email addCc(String cc) {
221+
this.cc.add(cc);
222+
return this;
223+
}
224+
225+
public Email addCc(String[] ccs) {
226+
this.cc.addAll(Arrays.asList(ccs));
227+
return this;
228+
}
229+
230+
public Email setCc(String[] ccs) {
231+
this.cc = new ArrayList<String>(Arrays.asList(ccs));
232+
return this;
233+
}
234+
235+
public String[] getCcs() {
236+
return this.cc.toArray(new String[this.cc.size()]);
237+
}
238+
212239
public Email setFrom(String from) {
213240
this.from = from;
214241
return this;

src/test/java/com/sendgrid/SendGridTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ public class SendGridTest {
6262
assertArrayEquals(correct, email.getToNames());
6363
}
6464

65+
@Test public void testAddCc() {
66+
email = new SendGrid.Email();
67+
68+
String address = "email@example.com";
69+
email.addCc(address);
70+
71+
String[] correct = {address};
72+
73+
assertArrayEquals(correct, email.getCcs());
74+
}
75+
6576
@Test public void testSetFrom() {
6677
email = new SendGrid.Email();
6778

0 commit comments

Comments
 (0)