2626import org .apache .http .entity .ContentType ;
2727
2828public 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 ;
0 commit comments