26
26
import org .apache .http .entity .ContentType ;
27
27
28
28
public class SendGrid {
29
- private static final String VERSION = "1.3.1 " ;
29
+ private static final String VERSION = "2.0.0 " ;
30
30
private static final String USER_AGENT = "sendgrid/" + VERSION + ";java" ;
31
31
32
32
private static final String PARAM_TO = "to[%d]" ;
@@ -89,14 +89,18 @@ public HttpEntity buildBody(Email email) {
89
89
String [] ccs = email .getCcs ();
90
90
String [] bccs = email .getBccs ();
91
91
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
+ }
92
96
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" ) );
94
98
for (int i = 0 , len = tonames .length ; i < len ; i ++)
95
99
builder .addTextBody (String .format (PARAM_TONAME , i ), tonames [i ], ContentType .create ("text/plain" , "UTF-8" ));
96
100
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" ) );
98
102
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" ) );
100
104
// Files
101
105
if (email .getAttachments ().size () > 0 ) {
102
106
Iterator it = email .getAttachments ().entrySet ().iterator ();
@@ -115,16 +119,16 @@ public HttpEntity buildBody(Email email) {
115
119
}
116
120
117
121
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" ) );
119
123
120
124
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" ) );
122
126
123
127
if (email .getFromName () != null && !email .getFromName ().isEmpty ())
124
128
builder .addTextBody (PARAM_FROMNAME , email .getFromName (), ContentType .create ("text/plain" , "UTF-8" ));
125
129
126
130
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" ) );
128
132
129
133
if (email .getSubject () != null && !email .getSubject ().isEmpty ())
130
134
builder .addTextBody (PARAM_SUBJECT , email .getSubject (), ContentType .create ("text/plain" , "UTF-8" ));
@@ -135,8 +139,8 @@ public HttpEntity buildBody(Email email) {
135
139
if (email .getText () != null && !email .getText ().isEmpty ())
136
140
builder .addTextBody (PARAM_TEXT , email .getText (), ContentType .create ("text/plain" , "UTF-8" ));
137
141
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" ));
140
144
141
145
return builder .build ();
142
146
}
@@ -181,13 +185,11 @@ public Email () {
181
185
}
182
186
183
187
public Email addTo (String to ) {
184
- this .smtpapi .addTo (to );
185
188
this .to .add (to );
186
189
return this ;
187
190
}
188
191
189
192
public Email addTo (String [] tos ) {
190
- this .smtpapi .addTos (tos );
191
193
this .to .addAll (Arrays .asList (tos ));
192
194
return this ;
193
195
}
@@ -198,7 +200,6 @@ public Email addTo(String to, String name) {
198
200
}
199
201
200
202
public Email setTo (String [] tos ) {
201
- this .smtpapi .setTos (tos );
202
203
this .to = new ArrayList <String >(Arrays .asList (tos ));
203
204
return this ;
204
205
}
@@ -207,7 +208,17 @@ public String[] getTos() {
207
208
return this .to .toArray (new String [this .to .size ()]);
208
209
}
209
210
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 ) {
211
222
this .toname .add (toname );
212
223
return this ;
213
224
}
@@ -318,13 +329,6 @@ public String getHtml() {
318
329
return this .html ;
319
330
}
320
331
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
-
328
332
public Email addSubstitution (String key , String [] val ) {
329
333
this .smtpapi .addSubstitutions (key , val );
330
334
return this ;
0 commit comments