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
658 lines (591 loc) · 16.5 KB
/
Mail.java
File metadata and controls
658 lines (591 loc) · 16.5 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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package com.sendgrid.helpers.mail;
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 com.sendgrid.helpers.mail.objects.ASM;
import com.sendgrid.helpers.mail.objects.Attachments;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
import com.sendgrid.helpers.mail.objects.MailSettings;
import com.sendgrid.helpers.mail.objects.Personalization;
import com.sendgrid.helpers.mail.objects.TrackingSettings;
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 Twilio SendGrid. Note that this object is
* not thread safe.
*/
@JsonInclude(Include.NON_DEFAULT)
public class Mail {
/** The email's from field. */
@JsonProperty("from")
public Email from;
/**
* The email's subject line. This is the global, or “message level”, subject of your email. This
* may be overridden by personalizations[x].subject.
*/
@JsonProperty("subject")
public String subject;
/**
* The email's personalization. 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.
*/
@JsonProperty("personalizations")
public List<Personalization> personalization;
/** The email's content. */
@JsonProperty("content")
public List<Content> content;
/** The email's attachments. */
@JsonProperty("attachments")
public List<Attachments> attachments;
/** The email's template ID. */
@JsonProperty("template_id")
public String templateId;
/**
* The email's sections. An object of key/value pairs that define block sections of code to be
* used as substitutions.
*/
@JsonProperty("sections")
public Map<String, String> sections;
/** The email's headers. */
@JsonProperty("headers")
public Map<String, String> headers;
/** The email's categories. */
@JsonProperty("categories")
public List<String> categories;
/**
* The email's custom arguments. 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.
*/
@JsonProperty("custom_args")
public Map<String, String> customArgs;
/**
* 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.
*/
@JsonProperty("send_at")
public long sendAt;
/**
* 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.
*/
@JsonProperty("batch_id")
public String batchId;
/** The email's unsubscribe handling object. */
@JsonProperty("asm")
public ASM asm;
/** The email's IP pool name. */
@JsonProperty("ip_pool_name")
public String ipPoolId;
/** The email's mail settings. */
@JsonProperty("mail_settings")
public MailSettings mailSettings;
/** The email's tracking settings. */
@JsonProperty("tracking_settings")
public TrackingSettings trackingSettings;
/** The email's reply to address. */
@JsonProperty("reply_to")
public Email replyTo;
/** The email's list of reply to addresses. */
@JsonProperty("reply_to_list")
public List<Email> replyToList;
private static final ObjectMapper SORTED_MAPPER = new ObjectMapper();
static {
SORTED_MAPPER.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
}
private <T> List<T> addToList(T element, List<T> defaultList) {
if (defaultList != null) {
defaultList.add(element);
return defaultList;
} else {
List<T> list = new ArrayList<T>();
list.add(element);
return list;
}
}
private <K, V> Map<K, V> addToMap(K key, V value, Map<K, V> defaultMap) {
if (defaultMap != null) {
defaultMap.put(key, value);
return defaultMap;
} else {
Map<K, V> map = new HashMap<K, V>();
map.put(key, value);
return map;
}
}
/** Construct a new Mail object. */
public Mail() {
return;
}
/**
* Construct a new Mail object.
*
* @param from the email's from address.
* @param subject the email's subject line.
* @param to the email's recipient.
* @param content the email's content.
*/
public Mail(Email from, String subject, Email to, Content content) {
this.setFrom(from);
this.setSubject(subject);
Personalization personalization = new Personalization();
personalization.addTo(to);
this.addPersonalization(personalization);
this.addContent(content);
}
/**
* 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.
*/
public void setFrom(Email from) {
this.from = from;
}
/**
* 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.
*
* @param subject the email's subject line.
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* 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.
*/
public void setASM(ASM asm) {
this.asm = asm;
}
/**
* 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.
*
* @param personalization a personalization.
*/
public void addPersonalization(Personalization personalization) {
this.personalization = addToList(personalization, this.personalization);
}
/**
* 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.
*/
public void addContent(Content content) {
Content newContent = new Content();
newContent.setType(content.getType());
newContent.setValue(content.getValue());
this.content = addToList(newContent, this.content);
}
/**
* 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<Attachments> getAttachments() {
return attachments;
}
/**
* Add attachments to the email.
*
* @param attachments attachments to add.
*/
public void addAttachments(Attachments attachments) {
Attachments newAttachment = new Attachments();
newAttachment.setContent(attachments.getContent());
newAttachment.setType(attachments.getType());
newAttachment.setFilename(attachments.getFilename());
newAttachment.setDisposition(attachments.getDisposition());
newAttachment.setContentId(attachments.getContentId());
this.attachments = addToList(newAttachment, this.attachments);
}
/**
* 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.
*/
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
/**
* 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.
*
* @param key the section's key.
* @param value the section's value.
*/
public void addSection(String key, String value) {
this.sections = addToMap(key, value, this.sections);
}
/**
* 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.
*/
public void addHeader(String key, String value) {
this.headers = addToMap(key, value, this.headers);
}
/**
* 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.
*/
public void addCategory(String category) {
this.categories = addToList(category, this.categories);
}
/**
* 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.
*
* @param key argument's key.
* @param value the argument's value.
*/
public void addCustomArg(String key, String value) {
this.customArgs = addToMap(key, value, this.customArgs);
}
/**
* 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 (Unix timestamp).
*
* @param sendAt the send at time.
*/
public void setSendAt(long sendAt) {
this.sendAt = sendAt;
}
/**
* Get the email's batch ID.
*
* @return the batch ID.
*/
@JsonProperty("batch_id")
public String getBatchId() {
return batchId;
}
/**
* Set the email's batch ID.
*
* @param batchId the batch ID.
*/
public void setBatchId(String batchId) {
this.batchId = batchId;
}
/**
* 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.
*/
public void setIpPoolId(String ipPoolId) {
this.ipPoolId = ipPoolId;
}
/**
* 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.
*/
public void setMailSettings(MailSettings mailSettings) {
this.mailSettings = mailSettings;
}
/**
* 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.
*/
public void setTrackingSettings(TrackingSettings trackingSettings) {
this.trackingSettings = trackingSettings;
}
/**
* 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.
*/
public void setReplyTo(Email replyTo) {
this.replyTo = replyTo;
}
/**
* Get the email's list of reply to addresses.
*
* @return the list of reply to addresses.
*/
public List<Email> getReplyToList() {
return replyToList;
}
/**
* Set the email's list of reply to addresses.
*
* @param replyToList the list of reply to addresses.
*/
public void setReplyToList(List<Email> replyToList) {
this.replyToList = replyToList;
}
/**
* Appends to the email's list of reply to addresses.
*
* @param replyTo the address to append to the list of reply to addresses.
*/
public void addReplyTo(Email replyTo) {
this.replyToList = addToList(replyTo, this.replyToList);
}
/**
* Create a string representation of the Mail object JSON.
*
* @return a JSON string.
* @throws IOException in case of a JSON marshal error.
*/
public String build() throws IOException {
try {
//ObjectMapper mapper = new ObjectMapper();
return SORTED_MAPPER.writeValueAsString(this);
} catch (IOException ex) {
throw ex;
}
}
/**
* Create a string representation 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;
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((batchId == null) ? 0 : batchId.hashCode());
result = prime * result + ((categories == null) ? 0 : categories.hashCode());
result = prime * result + ((customArgs == null) ? 0 : customArgs.hashCode());
result = prime * result + ((headers == null) ? 0 : headers.hashCode());
result = prime * result + ((ipPoolId == null) ? 0 : ipPoolId.hashCode());
result = prime * result + ((sections == null) ? 0 : sections.hashCode());
result = prime * result + (int) (sendAt ^ (sendAt >>> 32));
result = prime * result + ((subject == null) ? 0 : subject.hashCode());
result = prime * result + ((templateId == null) ? 0 : templateId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Mail other = (Mail) obj;
if (batchId == null) {
if (other.batchId != null) {
return false;
}
} else if (!batchId.equals(other.batchId)) {
return false;
}
if (categories == null) {
if (other.categories != null) {
return false;
}
} else if (!categories.equals(other.categories)) {
return false;
}
if (customArgs == null) {
if (other.customArgs != null) {
return false;
}
} else if (!customArgs.equals(other.customArgs)) {
return false;
}
if (headers == null) {
if (other.headers != null) {
return false;
}
} else if (!headers.equals(other.headers)) {
return false;
}
if (ipPoolId == null) {
if (other.ipPoolId != null) {
return false;
}
} else if (!ipPoolId.equals(other.ipPoolId)) {
return false;
}
if (sections == null) {
if (other.sections != null) {
return false;
}
} else if (!sections.equals(other.sections)) {
return false;
}
if (sendAt != other.sendAt) {
return false;
}
if (subject == null) {
if (other.subject != null) {
return false;
}
} else if (!subject.equals(other.subject)) {
return false;
}
if (templateId == null) {
if (other.templateId != null) {
return false;
}
} else if (!templateId.equals(other.templateId)) {
return false;
}
return true;
}
}