forked from sendgrid/sendgrid-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailTest.java
More file actions
317 lines (280 loc) · 17.2 KB
/
MailTest.java
File metadata and controls
317 lines (280 loc) · 17.2 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
package com.sendgrid.helpers;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
public class MailTest {
@Test
public void testHelloWorld() throws IOException {
Email from = new Email("test@example.com");
String subject = "Sending with SendGrid is Fun";
Email to = new Email("test@example.com");
Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
Mail mail = new Mail(from, subject, to, content);
Assert.assertEquals(mail.build(), "{\"from\":{\"email\":\"test@example.com\"},\"subject\":\"Sending with SendGrid is Fun\",\"personalizations\":[{\"to\":[{\"email\":\"test@example.com\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"and easy to do anywhere, even with Java\"}]}");
}
@Test
public void testKitchenSink() throws IOException {
Mail mail = new Mail();
Email fromEmail = new Email();
fromEmail.setName("Example User");
fromEmail.setEmail("test@example.com");
mail.setFrom(fromEmail);
mail.setSubject("Hello World from the SendGrid Java Library");
Personalization personalization = new Personalization();
Email to = new Email();
to.setName("Example User");
to.setEmail("test@example.com");
personalization.addTo(to);
to.setName("Example User");
to.setEmail("test@example.com");
personalization.addTo(to);
Email cc = new Email();
cc.setName("Example User");
cc.setEmail("test@example.com");
personalization.addCc(cc);
cc.setName("Example User");
cc.setEmail("test@example.com");
personalization.addCc(cc);
Email bcc = new Email();
bcc.setName("Example User");
bcc.setEmail("test@example.com");
personalization.addBcc(bcc);
bcc.setName("Example User");
bcc.setEmail("test@example.com");
personalization.addBcc(bcc);
personalization.setSubject("Hello World from the Personalized SendGrid Java Library");
personalization.addHeader("X-Test", "test");
personalization.addHeader("X-Mock", "true");
personalization.addSubstitution("%name%", "Example User");
personalization.addSubstitution("%city%", "Denver");
personalization.addCustomArg("user_id", "343");
personalization.addCustomArg("type", "marketing");
personalization.setSendAt(1443636843);
mail.addPersonalization(personalization);
Personalization personalization2 = new Personalization();
Email to2 = new Email();
to2.setName("Example User");
to2.setEmail("test@example.com");
personalization2.addTo(to2);
to2.setName("Example User");
to2.setEmail("test@example.com");
personalization2.addTo(to2);
Email fromEmail2 = new Email();
fromEmail2.setName("Example Sender");
fromEmail2.setEmail("sender@example.com");
personalization2.setFrom(fromEmail2);
Email cc2 = new Email();
cc2.setName("Example User");
cc2.setEmail("test@example.com");
personalization2.addCc(cc2);
cc2.setName("Example User");
cc2.setEmail("test@example.com");
personalization2.addCc(cc2);
Email bcc2 = new Email();
bcc2.setName("Example User");
bcc2.setEmail("test@example.com");
personalization2.addBcc(bcc2);
bcc2.setName("Example User");
bcc2.setEmail("test@example.com");
personalization2.addBcc(bcc2);
personalization2.setSubject("Hello World from the Personalized SendGrid Java Library");
personalization2.addHeader("X-Test", "test");
personalization2.addHeader("X-Mock", "true");
personalization2.addSubstitution("%name%", "Example User");
personalization2.addSubstitution("%city%", "Denver");
personalization2.addCustomArg("user_id", "343");
personalization2.addCustomArg("type", "marketing");
personalization2.setSendAt(1443636843);
mail.addPersonalization(personalization2);
Personalization personalization3 = new Personalization();
Email to3 = new Email();
to3.setName("Example User");
to3.setEmail("test@example.com");
personalization3.addTo(to3);
to3.setName("Example User");
to3.setEmail("test@example.com");
personalization3.addTo(to3);
Email fromEmail3 = new Email();
fromEmail3.setName("Example Sender2");
fromEmail3.setEmail("sender2@example.com");
personalization3.setFrom(fromEmail3);
Email cc3 = new Email();
cc3.setName("Example User");
cc3.setEmail("test@example.com");
personalization3.addCc(cc3);
cc3.setName("Example User");
cc3.setEmail("test@example.com");
personalization3.addCc(cc3);
Email bcc3 = new Email();
bcc3.setName("Example User");
bcc3.setEmail("test@example.com");
personalization3.addBcc(bcc3);
bcc3.setName("Example User");
bcc3.setEmail("test@example.com");
personalization3.addBcc(bcc3);
personalization3.setSubject("Hello World from the Personalized SendGrid Java Library");
personalization3.addHeader("X-Test", "test");
personalization3.addHeader("X-Mock", "true");
List<Map<String, String>> items = new ArrayList<>();
Map<String, String> item1 = new HashMap<>();
item1.put("text", "New Line Sneakers");
item1.put("price", "$ 79.95");
items.add(item1);
Map<String, String> item2 = new HashMap<>();
item2.put("text", "Old Line Sneakers");
item1.put("price", "$ 59.95");
items.add(item2);
personalization3.addDynamicTemplateData("name", "Example User");
personalization3.addDynamicTemplateData("city", "Denver");
personalization3.addDynamicTemplateData("items", items);
personalization3.addCustomArg("user_id", "343");
personalization3.addCustomArg("type", "marketing");
personalization3.setSendAt(1443636843);
mail.addPersonalization(personalization3);
Content content = new Content();
content.setType("text/plain");
content.setValue("some text here");
mail.addContent(content);
content.setType("text/html");
content.setValue("<html><body>some text here</body></html>");
mail.addContent(content);
Attachments attachments = new Attachments();
attachments.setContent("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12");
attachments.setType("application/pdf");
attachments.setFilename("balance_001.pdf");
attachments.setDisposition("attachment");
attachments.setContentId("Balance Sheet");
mail.addAttachments(attachments);
Attachments attachments2 = new Attachments();
attachments2.setContent("BwdW");
attachments2.setType("image/png");
attachments2.setFilename("banner.png");
attachments2.setDisposition("inline");
attachments2.setContentId("Banner");
mail.addAttachments(attachments2);
mail.setTemplateId("13b8f94f-bcae-4ec6-b752-70d6cb59f932");
mail.addSection("%section1%", "Substitution Text for Section 1");
mail.addSection("%section2%", "Substitution Text for Section 2");
mail.addHeader("X-Test1", "1");
mail.addHeader("X-Test2", "2");
mail.addCategory("May");
mail.addCategory("2016");
mail.addCustomArg("campaign", "welcome");
mail.addCustomArg("weekday", "morning");
mail.setSendAt(1443636842);
ASM asm = new ASM();
asm.setGroupId(99);
asm.setGroupsToDisplay(new int[] {4,5,6,7,8});
mail.setASM(asm);
// This must be a valid [batch ID](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work
// mail.setBatchId("sendgrid_batch_id");
mail.setIpPoolId("23");
MailSettings mailSettings = new MailSettings();
BccSettings bccSettings = new BccSettings();
bccSettings.setEnable(true);
bccSettings.setEmail("test@example.com");
mailSettings.setBccSettings(bccSettings);
Setting sandBoxMode = new Setting();
sandBoxMode.setEnable(true);
mailSettings.setSandboxMode(sandBoxMode);
Setting bypassListManagement = new Setting();
bypassListManagement.setEnable(true);
Setting bypassSpamManagement = new Setting();
bypassSpamManagement.setEnable(true);
Setting bypassBounceManagement = new Setting();
bypassBounceManagement.setEnable(true);
Setting bypassUnsubscribeManagement = new Setting();
bypassUnsubscribeManagement.setEnable(true);
mailSettings.setBypassListManagement(bypassListManagement);
mailSettings.setBypassSpamManagement(bypassSpamManagement);
mailSettings.setBypassBounceManagement(bypassBounceManagement);
mailSettings.setBypassUnsubscribeManagement(bypassUnsubscribeManagement);
FooterSetting footerSetting = new FooterSetting();
footerSetting.setEnable(true);
footerSetting.setText("Footer Text");
footerSetting.setHtml("<html><body>Footer Text</body></html>");
mailSettings.setFooterSetting(footerSetting);
SpamCheckSetting spamCheckSetting = new SpamCheckSetting();
spamCheckSetting.setEnable(true);
spamCheckSetting.setSpamThreshold(1);
spamCheckSetting.setPostToUrl("https://spamcatcher.sendgrid.com");
mailSettings.setSpamCheckSetting(spamCheckSetting);
mail.setMailSettings(mailSettings);
TrackingSettings trackingSettings = new TrackingSettings();
ClickTrackingSetting clickTrackingSetting = new ClickTrackingSetting();
clickTrackingSetting.setEnable(true);
clickTrackingSetting.setEnableText(false);
trackingSettings.setClickTrackingSetting(clickTrackingSetting);
OpenTrackingSetting openTrackingSetting = new OpenTrackingSetting();
openTrackingSetting.setEnable(true);
openTrackingSetting.setSubstitutionTag("Optional tag to replace with the open image in the body of the message");
trackingSettings.setOpenTrackingSetting(openTrackingSetting);
SubscriptionTrackingSetting subscriptionTrackingSetting = new SubscriptionTrackingSetting();
subscriptionTrackingSetting.setEnable(true);
subscriptionTrackingSetting.setText("text to insert into the text/plain portion of the message");
subscriptionTrackingSetting.setHtml("<html><body>html to insert into the text/html portion of the message</body></html>");
subscriptionTrackingSetting.setSubstitutionTag("Optional tag to replace with the open image in the body of the message");
trackingSettings.setSubscriptionTrackingSetting(subscriptionTrackingSetting);
GoogleAnalyticsSetting googleAnalyticsSetting = new GoogleAnalyticsSetting();
googleAnalyticsSetting.setEnable(true);
googleAnalyticsSetting.setCampaignSource("some source");
googleAnalyticsSetting.setCampaignTerm("some term");
googleAnalyticsSetting.setCampaignContent("some content");
googleAnalyticsSetting.setCampaignName("some name");
googleAnalyticsSetting.setCampaignMedium("some medium");
trackingSettings.setGoogleAnalyticsSetting(googleAnalyticsSetting);
mail.setTrackingSettings(trackingSettings);
Email replyTo = new Email();
replyTo.setName("Example User");
replyTo.setEmail("test@example.com");
mail.setReplyTo(replyTo);
Email replyToEmail1 = new Email();
replyToEmail1.setName("Reply To List 1");
replyToEmail1.setEmail("replyToList1@example.com");
Email replyToEmail2 = new Email();
replyToEmail2.setName("Reply To List 2");
replyToEmail2.setEmail("replyToList2@example.com");
List<Email> replyToList = new ArrayList<>();
replyToList.add(replyToEmail1);
replyToList.add(replyToEmail2);
mail.setReplyToList(replyToList);
Assert.assertEquals(mail.build(), "{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Hello World from the SendGrid Java Library\",\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"cc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"bcc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"subject\":\"Hello World from the Personalized SendGrid Java Library\",\"headers\":{\"X-Mock\":\"true\",\"X-Test\":\"test\"},\"substitutions\":{\"%city%\":\"Denver\",\"%name%\":\"Example User\"},\"custom_args\":{\"type\":\"marketing\",\"user_id\":\"343\"},\"send_at\":1443636843},{\"to\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"from\":{\"name\":\"Example Sender\",\"email\":\"sender@example.com\"},\"cc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"bcc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"subject\":\"Hello World from the Personalized SendGrid Java Library\",\"headers\":{\"X-Mock\":\"true\",\"X-Test\":\"test\"},\"substitutions\":{\"%city%\":\"Denver\",\"%name%\":\"Example User\"},\"custom_args\":{\"type\":\"marketing\",\"user_id\":\"343\"},\"send_at\":1443636843},{\"to\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"from\":{\"name\":\"Example Sender2\",\"email\":\"sender2@example.com\"},\"cc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"bcc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"subject\":\"Hello World from the Personalized SendGrid Java Library\",\"headers\":{\"X-Mock\":\"true\",\"X-Test\":\"test\"},\"custom_args\":{\"type\":\"marketing\",\"user_id\":\"343\"},\"dynamic_template_data\":{\"city\":\"Denver\",\"items\":[{\"price\":\"$ 59.95\",\"text\":\"New Line Sneakers\"},{\"text\":\"Old Line Sneakers\"}],\"name\":\"Example User\"},\"send_at\":1443636843}],\"content\":[{\"type\":\"text/plain\",\"value\":\"some text here\"},{\"type\":\"text/html\",\"value\":\"<html><body>some text here</body></html>\"}],\"attachments\":[{\"content\":\"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12\",\"type\":\"application/pdf\",\"filename\":\"balance_001.pdf\",\"disposition\":\"attachment\",\"content_id\":\"Balance Sheet\"},{\"content\":\"BwdW\",\"type\":\"image/png\",\"filename\":\"banner.png\",\"disposition\":\"inline\",\"content_id\":\"Banner\"}],\"template_id\":\"13b8f94f-bcae-4ec6-b752-70d6cb59f932\",\"sections\":{\"%section1%\":\"Substitution Text for Section 1\",\"%section2%\":\"Substitution Text for Section 2\"},\"headers\":{\"X-Test1\":\"1\",\"X-Test2\":\"2\"},\"categories\":[\"May\",\"2016\"],\"custom_args\":{\"campaign\":\"welcome\",\"weekday\":\"morning\"},\"send_at\":1443636842,\"asm\":{\"group_id\":99,\"groups_to_display\":[4,5,6,7,8]},\"ip_pool_name\":\"23\",\"mail_settings\":{\"bcc\":{\"enable\":true,\"email\":\"test@example.com\"},\"bypass_list_management\":{\"enable\":true},\"bypass_spam_management\":{\"enable\":true},\"bypass_bounce_management\":{\"enable\":true},\"bypass_unsubscribe_management\":{\"enable\":true},\"footer\":{\"enable\":true,\"text\":\"Footer Text\",\"html\":\"<html><body>Footer Text</body></html>\"},\"sandbox_mode\":{\"enable\":true},\"spam_check\":{\"enable\":true,\"threshold\":1,\"post_to_url\":\"https://spamcatcher.sendgrid.com\"}},\"tracking_settings\":{\"click_tracking\":{\"enable\":true,\"enable_text\":false},\"open_tracking\":{\"enable\":true,\"substitution_tag\":\"Optional tag to replace with the open image in the body of the message\"},\"subscription_tracking\":{\"enable\":true,\"text\":\"text to insert into the text/plain portion of the message\",\"html\":\"<html><body>html to insert into the text/html portion of the message</body></html>\",\"substitution_tag\":\"Optional tag to replace with the open image in the body of the message\"},\"ganalytics\":{\"enable\":true,\"utm_source\":\"some source\",\"utm_term\":\"some term\",\"utm_content\":\"some content\",\"utm_campaign\":\"some name\",\"utm_medium\":\"some medium\"}},\"reply_to\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"reply_to_list\":[{\"name\":\"Reply To List 1\",\"email\":\"replyToList1@example.com\"},{\"name\":\"Reply To List 2\",\"email\":\"replyToList2@example.com\"}]}");
}
@Test
public void addReplyToShouldAppend() {
Mail mail = new Mail();
Email replyTo1 = new Email("test1@example.com", "Test User1");
Email replyTo2 = new Email("test2@example.com", "Test User2");
mail.addReplyTo(replyTo1);
Assert.assertEquals(Collections.singletonList(replyTo1), mail.getReplyToList());
mail.addReplyTo(replyTo2);
Assert.assertEquals(Arrays.asList(replyTo1, replyTo2), mail.getReplyToList());
}
@Test
public void fromShouldReturnCorrectFrom() {
Mail mail = new Mail();
Email from = new Email();
mail.setFrom(from);
Assert.assertSame(from, mail.getFrom());
}
@Test
public void mailDeserialization() throws IOException {
Email to = new Email("foo@bar.com");
Content content = new Content("text/plain", "test");
Email from = new Email("no-reply@bar.com");
Mail mail = new Mail(from, "subject", to, content);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(mail);
Mail deserialized = mapper.readValue(json, Mail.class);
Assert.assertEquals(deserialized, mail);
}
}