forked from sendgrid/sendgrid-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMail.java
More file actions
572 lines (504 loc) · 15.4 KB
/
Mail.java
File metadata and controls
572 lines (504 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package com.sendgrid;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Class Mail builds an object that sends an email through SendGrid.
* Note that this object is not thread safe.
*/
@JsonInclude(Include.NON_DEFAULT)
public class Mail {
@JsonProperty("from")
private Email from;
@JsonProperty("subject")
private String subject;
@JsonProperty("personalizations")
private List<Personalization> personalization;
@JsonProperty("content")
private List<Content> content;
@JsonProperty("attachments")
private List<Attachment> attachments;
@JsonProperty("template_id")
private String templateId;
@JsonProperty("sections")
private Map<String, String> sections;
@JsonProperty("headers")
private Map<String, String> headers;
@JsonProperty("categories")
private List<String> categories;
@JsonProperty("custom_args")
private Map<String, String> customArgs;
@JsonProperty("send_at")
private long sendAt;
@JsonProperty("batch_id")
private String batchId;
@JsonProperty("asm")
private ASM asm;
@JsonProperty("ip_pool_name")
private String ipPoolId;
@JsonProperty("mail_settings")
private MailSettings mailSettings;
@JsonProperty("tracking_settings")
private TrackingSettings trackingSettings;
@JsonProperty("reply_to")
private Email replyTo;
private static final ObjectMapper SORTED_MAPPER = new ObjectMapper();
static {
SORTED_MAPPER.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
}
/** Construct a new Mail object. */
public Mail() {
}
/**
* Get the email's from address.
* @return the email's from address.
*/
@JsonProperty("from")
public Email getFrom() {
return this.from;
}
/**
* Set the email's from address.
* @param from the email's from address.
* @return this object.
*/
public Mail from(Email from) {
this.from = from;
return this;
}
/**
* Get the email's to address list.
* @return all the to addresses.
*/
@JsonIgnore
public List<Email> getTos() {
List<Email> l = new ArrayList<>();
if(this.getPersonalization() == null) {
return l;
}
for(Personalization p : this.getPersonalization()) {
l.addAll(p.getTos());
}
return l;
}
/**
* Add a to address. This is a convenience method for adding
* an address to the a personalization object. To addresses
* are located in the Personalization object. This method adds
* the address to the first personalization, creating it if
* necessary. If you would like to add the address to a different
* personalization object, please do so directly.
* @param to the to address
* @return this object.
*/
public Mail to(Email to) {
Personalization p;
if(this.personalization == null) {
this.personalization = new ArrayList<>();
}
if(this.personalization.size() == 0) {
p = new Personalization();
this.personalization.add(p);
} else {
p = this.personalization.get(0);
}
p.to(to);
return this;
}
/**
* Get the email's subject line.
* @return the email's subject line.
*/
@JsonProperty("subject")
public String getSubject() {
return subject;
}
/**
* Set the email's subject line. This is the global, or
* “message level”, subject of your email. This may
* be overridden by personalizations[x].subject.
* @param subject the email's subject line.
* @return this object.
*/
public Mail subject(String subject) {
this.subject = subject;
return this;
}
/**
* Get the email's unsubscribe handling object (ASM).
* @return the email's ASM.
*/
@JsonProperty("asm")
public ASM getASM() {
return asm;
}
/**
* Set the email's unsubscribe handling object (ASM).
* @param asm the email's ASM.
* @return this object.
*/
public Mail asm(ASM asm) {
this.asm = asm;
return this;
}
/**
* Get the email's personalizations. Content added to the returned
* list will be included when sent.
* @return the email's personalizations.
*/
@JsonProperty("personalizations")
public List<Personalization> getPersonalization() {
return personalization;
}
/**
* Add a personalization to the email. Each object within
* personalizations can be thought of as an envelope
* - it defines who should receive an individual message
* and how that message should be handled.
* @param personalization a personalization.
* @return this object.
*/
public Mail personalization(Personalization personalization) {
if (this.personalization == null) {
this.personalization = new ArrayList<>();
this.personalization.add(personalization);
} else {
this.personalization.add(personalization);
}
return this;
}
/**
* Get the email's content. Content added to the returned list
* will be included when sent.
* @return the email's content.
*/
@JsonProperty("content")
public List<Content> getContent() {
return content;
}
/**
* Add content to this email.
* @param content content to add to this email.
* @return this object.
*/
public Mail content(Content content) {
Content newContent = new Content()
.type(content.getType())
.value(content.getValue());
if (this.content == null) {
this.content = new ArrayList<Content>();
this.content.add(newContent);
} else {
this.content.add(newContent);
}
return this;
}
/**
* Get the email's attachments. Attachments added to the returned
* list will be included when sent.
* @return the email's attachments.
*/
@JsonProperty("attachments")
public List<Attachment> getAttachments() {
return attachments;
}
/**
* Add attachments to the email.
* @param attachment attachment to add.
* @return this object.
*/
public Mail attachment(Attachment attachment) {
Attachment newAttachment = new Attachment()
.content(attachment.getContent())
.type(attachment.getType())
.filename(attachment.getFilename())
.disposition(attachment.getDisposition())
.contentId(attachment.getContentId());
if (this.attachments == null) {
this.attachments = new ArrayList<Attachment>();
this.attachments.add(newAttachment);
} else {
this.attachments.add(newAttachment);
}
return this;
}
/**
* Get the email's template ID.
* @return the email's template ID.
*/
@JsonProperty("template_id")
public String getTemplateId() {
return this.templateId;
}
/**
* Set the email's template ID.
* @param templateId the email's template ID.
* @return this object.
*/
public Mail templateId(String templateId) {
this.templateId = templateId;
return this;
}
/**
* Get the email's sections. Sections added to the returned list
* will be included when sent.
* @return the email's sections.
*/
@JsonProperty("sections")
public Map<String, String> getSections() {
return sections;
}
/**
* Add a section to the email. A section is an object of key/value
* pairs that define block sections of code to be used as substitutions.
* @param key the section's key.
* @param value the section's value.
* @return this object.
*/
public Mail section(String key, String value) {
if (sections == null) {
sections = new HashMap<String, String>();
sections.put(key, value);
} else {
sections.put(key, value);
}
return this;
}
/**
* Get the email's headers. Headers added to the returned list
* will be included when sent.
* @return the email's headers.
*/
@JsonProperty("headers")
public Map<String, String> getHeaders() {
return headers;
}
/**
* Add a header to the email.
* @param key the header's key.
* @param value the header's value.
* @return this object.
*/
public Mail header(String key, String value) {
if (headers == null) {
headers = new HashMap<String, String>();
headers.put(key, value);
} else {
headers.put(key, value);
}
return this;
}
/**
* Get the email's categories. Categories added to the returned list
* will be included when sent.
* @return the email's categories.
*/
@JsonProperty("categories")
public List<String> getCategories() {
return categories;
}
/**
* Add a category to the email.
* @param category the category.
* @return this object.
*/
public Mail category(String category) {
if (categories == null) {
categories = new ArrayList<String>();
categories.add(category);
} else {
categories.add(category);
}
return this;
}
/**
* Get the email's custom arguments. Custom arguments added to the returned list
* will be included when sent.
* @return the email's custom arguments.
*/
@JsonProperty("custom_args")
public Map<String, String> getCustomArgs() {
return customArgs;
}
/**
* Add a custom argument to the email. An email's custom args
* are values that are specific to the entire send that will
* be carried along with the email and its activity data.
* Substitutions will not be made on custom arguments, so any
* string that is entered into this parameter will be assumed
* to be the custom argument that you would like to be used.
* This parameter is overridden by personalizations[x].custom_args
* if that parameter has been defined. Total custom args size
* may not exceed 10,000 bytes.
* @param key argument's key.
* @param value the argument's value.
* @return this object.
*/
public Mail customArg(String key, String value) {
if (customArgs == null) {
customArgs = new HashMap<String, String>();
customArgs.put(key, value);
} else {
customArgs.put(key, value);
}
return this;
}
/**
* Get the email's send at time (Unix timestamp).
* @return the email's send at time.
*/
@JsonProperty("send_at")
public long sendAt() {
return sendAt;
}
/**
* Set the email's send at time.
* A unix timestamp allowing you to specify when you want
* your email to be delivered. This may be overridden by
* the personalizations[x].send_at parameter. Scheduling
* more than 72 hours in advance is forbidden.
* @param sendAt the send at time.
* @return this object.
*/
public Mail sendAt(long sendAt) {
this.sendAt = sendAt;
return this;
}
/**
* Get the email's batch ID.
* @return the batch ID.
*/
@JsonProperty("batch_id")
public String getBatchId() {
return batchId;
}
/**
* Set the email's batch ID.
* This ID represents a batch of emails to be sent at the
* same time. Including a batch_id in your request allows
* you include this email in that batch, and also enables
* you to cancel or pause the delivery of that batch. For
* more information, see https://sendgrid.com/docs/API_Reference/Web_API_v3/cancel_schedule_send.
* @param batchId the batch ID.
* @return this object.
*/
public Mail batchId(String batchId) {
this.batchId = batchId;
return this;
}
/**
* Get the email's IP pool ID.
* @return the IP pool ID.
*/
@JsonProperty("ip_pool_name")
public String getIpPoolId() {
return ipPoolId;
}
/**
* Set the email's IP pool ID.
* @param ipPoolId the IP pool ID.
* @return this object.
*/
public Mail ipPoolId(String ipPoolId) {
this.ipPoolId = ipPoolId;
return this;
}
/**
* Get the email's settings.
* @return the settings.
*/
@JsonProperty("mail_settings")
public MailSettings getMailSettings() {
return mailSettings;
}
/**
* Set the email's settings.
* @param mailSettings the settings.
* @return this object.
*/
public Mail mailSettings(MailSettings mailSettings) {
this.mailSettings = mailSettings;
return this;
}
/**
* Get the email's tracking settings.
* @return the tracking settings.
*/
@JsonProperty("tracking_settings")
public TrackingSettings getTrackingSettings() {
return trackingSettings;
}
/**
* Set the email's tracking settings.
* @param trackingSettings the tracking settings.
* @return this object.
*/
public Mail trackingSettings(TrackingSettings trackingSettings) {
this.trackingSettings = trackingSettings;
return this;
}
/**
* Get the email's reply to address.
* @return the reply to address.
*/
@JsonProperty("reply_to")
public Email getReplyto() {
return replyTo;
}
/**
* Set the email's reply to address.
* @param replyTo the reply to address.
* @return this object.
*/
public Mail replyTo(Email replyTo) {
this.replyTo = replyTo;
return this;
}
/**
* Create a string represenation of the Mail object JSON.
* @return a JSON string.
* @throws IOException in case of a JSON marshal error.
*/
protected String build() throws IOException {
try {
//ObjectMapper mapper = new ObjectMapper();
return SORTED_MAPPER.writeValueAsString(this);
} catch (IOException ex) {
throw ex;
}
}
/**
* Create a string represenation of the Mail object JSON and pretty print it.
* @return a pretty JSON string.
* @throws IOException in case of a JSON marshal error.
*/
public String buildPretty() throws IOException {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
} catch (IOException ex) {
throw ex;
}
}
/**
* Send the email.
* @param sg the SendGrid instance to use.
* @return the response object.
* @throws IOException in case of a marshal, or network error.
*/
public Response send(SendGrid sg) throws IOException {
Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(this.build());
return sg.send(request);
}
}