Skip to content

Commit f76e38f

Browse files
author
elbuo8
committed
Explicit calling of SMTPAPI through addSmtpApiTo
1 parent cb63d82 commit f76e38f

File tree

3 files changed

+77
-40
lines changed

3 files changed

+77
-40
lines changed

README.md

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ This Java module allows you to quickly and easily send emails through SendGrid u
44

55
[![BuildStatus](https://travis-ci.org/sendgrid/sendgrid-java.png?branch=master)](https://travis-ci.org/sendgrid/sendgrid-java)
66

7+
### Warning
8+
9+
Version ``2.x.x``, behaves differently in the ``addTo`` method. In the past this method defaulted to using the ``SMTPAPI`` header. Now you must explicitly call the ``addSmtpApiTo`` method. More on the ``SMTPAPI`` section.
10+
711
```java
812
// SendGridExample.java
913
import com.sendgrid.*;
@@ -46,7 +50,7 @@ Add the following to your build.gradle file in the root of your project.
4650
...
4751
dependencies {
4852
...
49-
compile 'com.sendgrid:sendgrid-java:1.2.1'
53+
compile 'com.sendgrid:sendgrid-java:2.0.0'
5054
}
5155
5256
repositories {
@@ -175,72 +179,89 @@ sendgrid.setClient(http);
175179

176180
The mail object extends the SMTPAPI object which is found in [STMAPI-Java](https://github.com/sendgrid/smtpapi-java).
177181

182+
```java
183+
email.getSMTPAPI();
184+
```
185+
186+
### Recipients
187+
188+
```java
189+
email.addSmtpApiTo("[email protected]");
190+
// or
191+
email.addSmtpApiTo(["[email protected]"]);
192+
```
193+
194+
178195
### [Substitutions](http://sendgrid.com/docs/API_Reference/SMTP_API/substitution_tags.html)
179196

180197
```java
181-
header.addSubstitution("key", "value");
198+
email.addSubstitution("key", "value");
182199
// or
183-
header.setSubstitutions("key", ["value1", "value2"]);
200+
email.setSubstitutions("key", ["value1", "value2"]);
184201

185202
JSONObject subs = header.getSubstitutions();
186203
```
187204

188205
### [Unique Arguments](http://sendgrid.com/docs/API_Reference/SMTP_API/unique_arguments.html)
189206

190207
```java
191-
header.addUuniqueAarg("key", "value");
208+
email.addUniqueAarg("key", "value");
192209
// or
193210
Map map = new HashMap<String, String>();
194211
map.put("unique", "value");
195-
header.setUniqueArgs(map);
212+
email.setUniqueArgs(map);
196213
// or
197214
JSONObject map = new JSONObject();
198215
map.put("unique", "value");
199-
header.setUniqueArgs(map);
216+
email.setUniqueArgs(map);
217+
// or
218+
email.setUniqueArgs(map);
200219

201-
JSONObject args = header.getUniqueArgs();
220+
JSONObject args = email.getUniqueArgs();
202221
```
203222
### [Categories](http://sendgrid.com/docs/API_Reference/SMTP_API/categories.html)
204223

205224
```java
206-
header.addCategory("category");
225+
email.addCategory("category");
226+
// or
227+
email.addCategory(["categories"]);
207228
// or
208-
header.addCategory(["categories"]);
229+
email.setCategories(["category1", "category2"]);
209230
// or
210-
header.setCategories(["category1", "category2"]);
231+
email.setCategories(["category1", category2"]);
211232
212-
String[] cats = header.getCategories();
233+
String[] cats = email.getCategories();
213234
```
214235
215236
### [Sections](http://sendgrid.com/docs/API_Reference/SMTP_API/section_tags.html)
216237
217238
```java
218-
header.addSection("key", "section");
239+
email.addSection("key", "section");
219240
// or
220241
Map newSec = new HashMap();
221242
newSec.put("-section-", "value");
222-
header.setSections(newSec);
243+
email.setSections(newSec);
223244
// or
224245
JSONObject newSec = new JSONObject();
225246
newSec.put("-section-", "value");
226-
header.setSections(newSec);
247+
email.setSections(newSec);
227248
228-
JSONObject sections = header.getSections();
249+
JSONObject sections = email.getSections();
229250
```
230251
231252
### [Filters](http://sendgrid.com/docs/API_Reference/SMTP_API/apps.html)
232253
233254
```java
234-
header.addFilter("filter", "setting", "value");
235-
header.addFilter("filter", "setting", 1);
255+
email.addFilter("filter", "setting", "value");
256+
email.addFilter("filter", "setting", 1);
236257
237-
JSONObject filters = header.getFilters();
258+
JSONObject filters = email.getFilters();
238259
```
239260
240261
### Get Headers
241262
242263
```java
243-
String headers = header.jsonString();
264+
String headers = email.jsonString();
244265
```
245266
246267
### Filters/Apps

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

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.apache.http.entity.ContentType;
2727

2828
public class SendGrid {
29-
private static final String VERSION = "1.3.1";
29+
private static final String VERSION = "2.0.0";
3030
private static final String USER_AGENT = "sendgrid/" + VERSION + ";java";
3131

3232
private static final String PARAM_TO = "to[%d]";
@@ -89,14 +89,18 @@ public HttpEntity buildBody(Email email) {
8989
String[] ccs = email.getCcs();
9090
String[] bccs = email.getBccs();
9191

92+
// If SMTPAPI Header is used, To is still required. #workaround.
93+
if (tos.length == 0) {
94+
builder.addTextBody(String.format(PARAM_TO, 0), email.getFrom(), ContentType.create("text/plain", "UTF-8"));
95+
}
9296
for (int i = 0, len = tos.length; i < len; i++)
93-
builder.addTextBody(String.format(PARAM_TO, i), tos[i]);
97+
builder.addTextBody(String.format(PARAM_TO, i), tos[i], ContentType.create("text/plain", "UTF-8"));
9498
for (int i = 0, len = tonames.length; i < len; i++)
9599
builder.addTextBody(String.format(PARAM_TONAME, i), tonames[i], ContentType.create("text/plain", "UTF-8"));
96100
for (int i = 0, len = ccs.length; i < len; i++)
97-
builder.addTextBody(String.format(PARAM_CC, i), ccs[i]);
101+
builder.addTextBody(String.format(PARAM_CC, i), ccs[i], ContentType.create("text/plain", "UTF-8"));
98102
for (int i = 0, len = bccs.length; i < len; i++)
99-
builder.addTextBody(String.format(PARAM_BCC, i), bccs[i]);
103+
builder.addTextBody(String.format(PARAM_BCC, i), bccs[i], ContentType.create("text/plain", "UTF-8"));
100104
// Files
101105
if (email.getAttachments().size() > 0) {
102106
Iterator it = email.getAttachments().entrySet().iterator();
@@ -115,16 +119,16 @@ public HttpEntity buildBody(Email email) {
115119
}
116120

117121
if (email.getHeaders().size() > 0)
118-
builder.addTextBody(PARAM_HEADERS, new JSONObject(email.getHeaders()).toString());
122+
builder.addTextBody(PARAM_HEADERS, new JSONObject(email.getHeaders()).toString(), ContentType.create("text/plain", "UTF-8"));
119123

120124
if (email.getFrom() != null && !email.getFrom().isEmpty())
121-
builder.addTextBody(PARAM_FROM, email.getFrom());
125+
builder.addTextBody(PARAM_FROM, email.getFrom(), ContentType.create("text/plain", "UTF-8"));
122126

123127
if (email.getFromName() != null && !email.getFromName().isEmpty())
124128
builder.addTextBody(PARAM_FROMNAME, email.getFromName(), ContentType.create("text/plain", "UTF-8"));
125129

126130
if (email.getReplyTo() != null && !email.getReplyTo().isEmpty())
127-
builder.addTextBody(PARAM_REPLYTO, email.getReplyTo());
131+
builder.addTextBody(PARAM_REPLYTO, email.getReplyTo(), ContentType.create("text/plain", "UTF-8"));
128132

129133
if (email.getSubject() != null && !email.getSubject().isEmpty())
130134
builder.addTextBody(PARAM_SUBJECT, email.getSubject(), ContentType.create("text/plain", "UTF-8"));
@@ -135,8 +139,8 @@ public HttpEntity buildBody(Email email) {
135139
if (email.getText() != null && !email.getText().isEmpty())
136140
builder.addTextBody(PARAM_TEXT, email.getText(), ContentType.create("text/plain", "UTF-8"));
137141

138-
if (!email.getSMTPAPI().jsonString().equals("{}"))
139-
builder.addTextBody(PARAM_XSMTPAPI, email.getSMTPAPI().jsonString());
142+
if (!email.smtpapi.jsonString().equals("{}"))
143+
builder.addTextBody(PARAM_XSMTPAPI, email.smtpapi.jsonString(), ContentType.create("text/plain", "UTF-8"));
140144

141145
return builder.build();
142146
}
@@ -181,13 +185,11 @@ public Email () {
181185
}
182186

183187
public Email addTo(String to) {
184-
this.smtpapi.addTo(to);
185188
this.to.add(to);
186189
return this;
187190
}
188191

189192
public Email addTo(String[] tos) {
190-
this.smtpapi.addTos(tos);
191193
this.to.addAll(Arrays.asList(tos));
192194
return this;
193195
}
@@ -198,7 +200,6 @@ public Email addTo(String to, String name) {
198200
}
199201

200202
public Email setTo(String[] tos) {
201-
this.smtpapi.setTos(tos);
202203
this.to = new ArrayList<String>(Arrays.asList(tos));
203204
return this;
204205
}
@@ -207,7 +208,17 @@ public String[] getTos() {
207208
return this.to.toArray(new String[this.to.size()]);
208209
}
209210

210-
public Email addToName(String toname) {
211+
public Email addSmtpApiTo(String to) {
212+
this.smtpapi.addTo(to);
213+
return this;
214+
}
215+
216+
public Email addSmtpApiTo(String[] to) {
217+
this.smtpapi.addTos(to);
218+
return this;
219+
}
220+
221+
public Email addToName(String toname) {
211222
this.toname.add(toname);
212223
return this;
213224
}
@@ -318,13 +329,6 @@ public String getHtml() {
318329
return this.html;
319330
}
320331

321-
public Email dropSMTPAPITos() {
322-
JSONObject oldHeader = new JSONObject(this.smtpapi.jsonString());
323-
oldHeader.remove("to");
324-
this.smtpapi = new SMTPAPI(oldHeader);
325-
return this;
326-
}
327-
328332
public Email addSubstitution(String key, String[] val) {
329333
this.smtpapi.addSubstitutions(key, val);
330334
return this;

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,16 @@ public class SendGridTest {
139139

140140
assertEquals(correct, email.getHeaders());
141141
}
142-
}
142+
143+
@Test public void testSmtpapiToHeader() {
144+
email = new SendGrid.Email();
145+
146+
String[] expected = {"[email protected]"};
147+
148+
email.getSMTPAPI().addTo(expected[0]);
149+
String[] result = email.getSMTPAPI().getTos();
150+
151+
assertArrayEquals(expected, result);
152+
}
153+
154+
}

0 commit comments

Comments
 (0)