From d1606881a74c28d292c52b2522335faf1c27a18a Mon Sep 17 00:00:00 2001 From: David An Date: Tue, 14 Jan 2025 17:11:10 -0500 Subject: [PATCH 1/3] support reply_to_list --- .../java/com/sendgrid/helpers/mail/Mail.java | 22 +++++++++++++++++++ .../java/com/sendgrid/helpers/MailTest.java | 13 ++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sendgrid/helpers/mail/Mail.java b/src/main/java/com/sendgrid/helpers/mail/Mail.java index ea2b9739..2938baa5 100644 --- a/src/main/java/com/sendgrid/helpers/mail/Mail.java +++ b/src/main/java/com/sendgrid/helpers/mail/Mail.java @@ -117,6 +117,10 @@ public class Mail { @JsonProperty("reply_to") public Email replyTo; + /** The email's list of reply to addresses. */ + @JsonProperty("reply_to_list") + public List replyToList; + private static final ObjectMapper SORTED_MAPPER = new ObjectMapper(); static { @@ -505,6 +509,24 @@ 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 getReplyToList() { + return replyToList; + } + + /** + * Set the email's list of reply to addresses. + * + * @param replyToList the list of reply to addresses. + */ + public void setReplyToList(List replyToList) { + this.replyToList = replyToList; + } + /** * Create a string representation of the Mail object JSON. * diff --git a/src/test/java/com/sendgrid/helpers/MailTest.java b/src/test/java/com/sendgrid/helpers/MailTest.java index 22c7f8db..fcfe8df9 100644 --- a/src/test/java/com/sendgrid/helpers/MailTest.java +++ b/src/test/java/com/sendgrid/helpers/MailTest.java @@ -264,7 +264,18 @@ public void testKitchenSink() throws IOException { replyTo.setEmail("test@example.com"); mail.setReplyTo(replyTo); - 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\":\"some text here\"}],\"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\":\"Footer Text\"},\"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 to insert into the text/html portion of the message\",\"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\"}}"); + 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 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\":\"some text here\"}],\"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\":\"Footer Text\"},\"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 to insert into the text/html portion of the message\",\"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 From 06b137ca8cccf5789738c7196d4a585dface2921 Mon Sep 17 00:00:00 2001 From: David An Date: Wed, 15 Jan 2025 09:21:00 -0500 Subject: [PATCH 2/3] add Mail.addReplyTo() method --- src/main/java/com/sendgrid/helpers/mail/Mail.java | 9 +++++++++ src/test/java/com/sendgrid/helpers/MailTest.java | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/com/sendgrid/helpers/mail/Mail.java b/src/main/java/com/sendgrid/helpers/mail/Mail.java index 2938baa5..72a21025 100644 --- a/src/main/java/com/sendgrid/helpers/mail/Mail.java +++ b/src/main/java/com/sendgrid/helpers/mail/Mail.java @@ -527,6 +527,15 @@ public void setReplyToList(List 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. * diff --git a/src/test/java/com/sendgrid/helpers/MailTest.java b/src/test/java/com/sendgrid/helpers/MailTest.java index fcfe8df9..9fbdc5c8 100644 --- a/src/test/java/com/sendgrid/helpers/MailTest.java +++ b/src/test/java/com/sendgrid/helpers/MailTest.java @@ -8,6 +8,8 @@ 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; @@ -278,6 +280,18 @@ public void testKitchenSink() throws IOException { 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\":\"some text here\"}],\"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\":\"Footer Text\"},\"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 to insert into the text/html portion of the message\",\"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(); From 834fe094c50e330e690d8087783f2fc6ffc524c1 Mon Sep 17 00:00:00 2001 From: David An Date: Wed, 15 Jan 2025 09:24:26 -0500 Subject: [PATCH 3/3] create example of multiple reply-tos --- examples/helpers/mail/Example.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/examples/helpers/mail/Example.java b/examples/helpers/mail/Example.java index 174e38ba..b2696aeb 100644 --- a/examples/helpers/mail/Example.java +++ b/examples/helpers/mail/Example.java @@ -241,6 +241,22 @@ public static Mail buildHelloEmail() { return mail; } + // Multiple Reply-To addresses on a Mail + public static Mail buildMultipleReplyToEmail() { + Email from = new Email("from@example.com"); + String subject = "Hello World from the Twilio SendGrid Java Library"; + Email to = new Email("to@example.com"); + Content content = new Content("text/plain", "some text here"); + Mail mail = new Mail(from, subject, to, content); + + Email replyTo1 = new Email("replyto1@example.com"); + Email replyTo2 = new Email("replyto2@example.com"); + mail.setReplyTo(Arrays.asList(replyTo1, replyTo2)); + + return mail; + } + + public static void baselineExample() throws IOException { final Mail helloWorld = buildHelloEmail(); send(helloWorld); @@ -256,6 +272,11 @@ public static void dynamicTemplateExample() throws IOException { send(dynamicTemplate); } + public static void MultipleReplyToExample() throws IOException { + final Mail multipleReplyTo = buildMultipleReplyToEmail(); + send(multipleReplyTo); + } + private static void send(final Mail mail) throws IOException { final SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY")); sg.addRequestHeader("X-Mock", "true");