77import com .mashape .unirest .http .JsonNode ;
88
99import java .util .ArrayList ;
10+ import java .util .Arrays ;
1011import java .util .HashMap ;
1112import java .util .Iterator ;
1213import java .util .Scanner ;
@@ -52,71 +53,146 @@ public SendGrid.Response send(Email email) throws SendGridException {
5253 }
5354
5455 public static class Email {
55- private static final String PARAM_TO = "to" ;
56+ private static final String PARAM_TO = "to[%d]" ;
57+ private static final String PARAM_TONAME = "toname[%d]" ;
5658 private static final String PARAM_FROM = "from" ;
5759 private static final String PARAM_FROMNAME = "fromname" ;
5860 private static final String PARAM_REPLYTO = "replyto" ;
59- private static final String PARAM_BCCS = "bcc[%d]" ;
61+ private static final String PARAM_BCC = "bcc[%d]" ;
6062 private static final String PARAM_SUBJECT = "subject" ;
6163 private static final String PARAM_HTML = "html" ;
6264 private static final String PARAM_TEXT = "text" ;
6365 private static final String PARAM_FILES = "files[%s]" ;
6466 private static final String PARAM_XSMTPAPI = "x-smtpapi" ;
6567 private static final String PARAM_HEADERS = "headers" ;
6668
67- public SMTPAPI smtpapi ;
68- SMTPAPI header = new SMTPAPI ();
69-
70- public String to ;
71- public String from ;
72- public String fromname ;
73- public String replyto ;
74- public String subject ;
75- public String text ;
76- public String html ;
77- public ArrayList <String > bcc = new ArrayList <String >();
78- public Map attachments = new HashMap ();
79- public Map headers = new HashMap ();
69+ private SMTPAPI smtpapi ;
70+ private ArrayList <String > to ;
71+ private ArrayList <String > toname ;
72+ private String from ;
73+ private String fromname ;
74+ private String replyto ;
75+ private String subject ;
76+ private String text ;
77+ private String html ;
78+ private ArrayList <String > bcc ;
79+ private Map attachments ;
80+ private Map headers ;
8081
8182 public Email () {
8283 this .smtpapi = new SMTPAPI ();
84+ this .to = new ArrayList <String >();
85+ this .toname = new ArrayList <String >();
86+ this .bcc = new ArrayList <String >();
87+ this .attachments = new HashMap ();
88+ this .headers = new HashMap ();
8389 }
8490
8591 public Email addTo (String to ) {
8692 this .smtpapi .addTo (to );
93+ this .to .add (to );
94+ return this ;
95+ }
96+
97+ public Email addTos (String [] tos ) {
98+ this .smtpapi .addTos (tos );
99+ this .to .addAll (Arrays .asList (tos ));
100+ return this ;
101+ }
102+
103+ public Email setTos (String [] tos ) {
104+ this .smtpapi .setTos (tos );
105+ this .to = new ArrayList (Arrays .asList (tos ));
106+ return this ;
107+ }
108+
109+ public String [] getTos () {
110+ return this .to .toArray (new String [this .to .size ()]);
111+ }
112+
113+ public Email addToName (String toname ) {
114+ this .toname .add (toname );
115+ return this ;
116+ }
117+
118+ public Email addToNames (String [] tonames ) {
119+ this .toname .addAll (Arrays .asList (tonames ));
120+ return this ;
121+ }
122+
123+ public Email setToNames (String [] tonames ) {
124+ this .toname = new ArrayList (Arrays .asList (tonames ));
87125 return this ;
88126 }
89127
128+ public String [] getToNames () {
129+ return this .toname .toArray (new String [this .toname .size ()]);
130+ }
131+
90132 public Email setFrom (String from ) {
91133 this .from = from ;
92134 return this ;
93135 }
94136
137+ public String getFrom () {
138+ return this .from ;
139+ }
140+
95141 public Email setFromName (String fromname ) {
96142 this .fromname = fromname ;
97143 return this ;
98144 }
99145
146+ public String getFromName () {
147+ return this .fromname ;
148+ }
149+
100150 public Email setReplyTo (String replyto ) {
101151 this .replyto = replyto ;
102152 return this ;
103153 }
104154
155+ public String getReplyTo () {
156+ return this .replyto ;
157+ }
158+
105159 public Email addBcc (String bcc ) {
106160 this .bcc .add (bcc );
107161 return this ;
108162 }
109163
164+ public Email addBccs (String [] bccs ) {
165+ this .bcc .addAll (Arrays .asList (bccs ));
166+ return this ;
167+ }
168+
169+ public Email setBccs (String [] bccs ) {
170+ this .bcc = new ArrayList (Arrays .asList (bccs ));
171+ return this ;
172+ }
173+
174+ public String [] getBccs () {
175+ return this .bcc .toArray (new String [this .bcc .size ()]);
176+ }
177+
110178 public Email setSubject (String subject ) {
111179 this .subject = subject ;
112180 return this ;
113181 }
114182
183+ public String getSubject () {
184+ return this .subject ;
185+ }
186+
115187 public Email setText (String text ) {
116188 this .text = text ;
117189 return this ;
118190 }
119191
192+ public String getText () {
193+ return this .text ;
194+ }
195+
120196 public Email setHtml (String html ) {
121197 this .html = html ;
122198 return this ;
@@ -127,26 +203,46 @@ public Email addSubstitution(String key, String[] val) {
127203 return this ;
128204 }
129205
206+ public JSONObject getSubstitutions () {
207+ return this .smtpapi .getSubstitutions ();
208+ }
209+
130210 public Email addUniqueArg (String key , String val ) {
131211 this .smtpapi .addUniqueArg (key , val );
132212 return this ;
133213 }
134214
215+ public JSONObject getUniqueArgs () {
216+ return this .smtpapi .getUniqueArgs ();
217+ }
218+
135219 public Email addCategory (String category ) {
136220 this .smtpapi .addCategory (category );
137221 return this ;
138222 }
139223
224+ public String [] getCategories () {
225+ return this .smtpapi .getCategories ();
226+ }
227+
140228 public Email addSection (String key , String val ) {
141229 this .smtpapi .addSection (key , val );
142230 return this ;
143231 }
144232
233+ public JSONObject getSections () {
234+ return this .smtpapi .getSections ();
235+ }
236+
145237 public Email addFilter (String filter_name , String parameter_name , String parameter_value ) {
146238 this .smtpapi .addFilter (filter_name , parameter_name , parameter_value );
147239 return this ;
148240 }
149241
242+ public JSONObject getFilters () {
243+ return this .smtpapi .getFilters ();
244+ }
245+
150246 public Email addAttachment (String name , File file ) {
151247 this .attachments .put (name , file );
152248 return this ;
@@ -172,62 +268,49 @@ public Email addHeader(String key, String val) {
172268 return this ;
173269 }
174270
271+ public Map getHeaders () {
272+ return this .headers ;
273+ }
274+
275+ public SMTPAPI getSMTPAPI () {
276+ return this .smtpapi ;
277+ }
278+
175279 public Map toWebFormat () {
176280 Map body = new HashMap ();
177281
178- // updateMissingTo - There needs to be at least 1 to address,
179- // or else the mail won't send.
180- if ((this .to == null || this .to .isEmpty ()) && this .from != null && !this .from .isEmpty ()) {
181- String value = this .from ;
182- body .put (PARAM_TO , value );
183- }
282+ for (int i = 0 ; i < this .to .size (); i ++)
283+ body .put (String .format (PARAM_TO , i ), this .to .get (i ));
184284
185- if (this .from != null && !this .from .isEmpty ()) {
186- String value = this .from ;
187- body .put (PARAM_FROM , value );
188- }
285+ for (int i = 0 ; i < this .toname .size (); i ++)
286+ body .put (String .format (PARAM_TONAME , i ), this .toname .get (i ));
189287
190- if (this .fromname != null && !this .fromname .isEmpty ()) {
191- String value = this .fromname ;
192- body .put (PARAM_FROMNAME , value );
193- }
288+ for (int i = 0 ; i < this .bcc .size (); i ++)
289+ body .put (String .format (PARAM_BCC , i ), this .bcc .get (i ));
194290
195- if (this .replyto != null && !this .replyto .isEmpty ()) {
196- String value = this .replyto ;
197- body .put (PARAM_REPLYTO , value );
198- }
291+ if (this .from != null && !this .from .isEmpty ())
292+ body .put (PARAM_FROM , this .from );
199293
200- for (int i = 0 ; i < this .bcc .size (); i ++) {
201- String key = String .format (PARAM_BCCS , i );
202- String value = this .bcc .get (i );
203- body .put (key , value );
204- }
294+ if (this .fromname != null && !this .fromname .isEmpty ())
295+ body .put (PARAM_FROMNAME , this .fromname );
205296
206- if (this .subject != null && !this .subject .isEmpty ()) {
207- String value = this .subject ;
208- body .put (PARAM_SUBJECT , value );
209- }
297+ if (this .replyto != null && !this .replyto .isEmpty ())
298+ body .put (PARAM_REPLYTO , this .replyto );
210299
211- if (this .text != null && !this .text .isEmpty ()) {
212- String value = this .text ;
213- body .put (PARAM_TEXT , value );
214- }
300+ if (this .subject != null && !this .subject .isEmpty ())
301+ body .put (PARAM_SUBJECT , this .subject );
215302
216- if (this .html != null && !this .html .isEmpty ()) {
217- String value = this .html ;
218- body .put (PARAM_HTML , value );
219- }
303+ if (this .text != null && !this .text .isEmpty ())
304+ body .put (PARAM_TEXT , this .text );
220305
221- if (!this .headers .isEmpty ()) {
222- JSONObject json_headers = new JSONObject (this .headers );
223- String serialized_headers = json_headers .toString ();
224- body .put (PARAM_HEADERS , serialized_headers );
225- }
306+ if (this .html != null && !this .html .isEmpty ())
307+ body .put (PARAM_HTML , this .html );
226308
227- if (!this .smtpapi .jsonString ().equals ("{}" )) {
228- String value = this .smtpapi .jsonString ();
229- body .put (PARAM_XSMTPAPI , value );
230- }
309+ if (!this .headers .isEmpty ())
310+ body .put (PARAM_HEADERS , new JSONObject (this .headers ).toString ());
311+
312+ if (!this .smtpapi .jsonString ().equals ("{}" ))
313+ body .put (PARAM_XSMTPAPI , this .smtpapi .jsonString ());
231314
232315 if (this .attachments .size () > 0 ) {
233316 Iterator it = this .attachments .entrySet ().iterator ();
0 commit comments