From d2a90873ece5a6d1b5859037b53e734dada0e845 Mon Sep 17 00:00:00 2001 From: Ryan Fleming Date: Tue, 15 Mar 2022 15:08:32 -0400 Subject: [PATCH 1/2] Add support for multiple Reply-Tos --- src/SendGrid/Helpers/Mail/SendGridMessage.cs | 50 +++++++++++++++++++ tests/SendGrid.Tests/Integration.cs | 17 +++++++ .../WhenCreatingASendGridMessage.cs | 14 ++++++ 3 files changed, 81 insertions(+) diff --git a/src/SendGrid/Helpers/Mail/SendGridMessage.cs b/src/SendGrid/Helpers/Mail/SendGridMessage.cs index c92a636c7..01eab4d41 100644 --- a/src/SendGrid/Helpers/Mail/SendGridMessage.cs +++ b/src/SendGrid/Helpers/Mail/SendGridMessage.cs @@ -138,6 +138,12 @@ public class SendGridMessage [JsonProperty(PropertyName = "reply_to")] public EmailAddress ReplyTo { get; set; } + /// + /// Gets or sets a list of objects of email objects containing the email address and name of the individuals who should receive responses to your email. + /// + [JsonProperty(PropertyName = "reply_to_list")] + public List ReplyTos { get; set; } + /// /// Add a recipient email. /// @@ -318,6 +324,50 @@ public void AddHeaders(Dictionary headers, int personalizationIn personalization.Headers.Union(headers).ToDictionary(pair => pair.Key, pair => pair.Value); } + /// + /// Add a reply-to email. + /// + /// Specify the recipient's email. + /// Specify the recipient's name. + /// Thrown when the email parameter is null or whitespace + public void AddReplyTo(string email, string name = null) + { + if (string.IsNullOrWhiteSpace(email)) + throw new ArgumentNullException("email"); + + this.AddReplyTo(new EmailAddress(email, name)); + } + + /// + /// Add a reply-to email. + /// + /// An email recipient that may contain the recipient’s name, but must always contain the recipient’s email. + /// Thrown when the email parameter is null + public void AddReplyTo(EmailAddress email) + { + if (email == null) + throw new ArgumentNullException("email"); + + AddReplyTos(new List { email }); + } + + /// + /// Add reply-to recipients. + /// + /// A list of reply-to recipients. Each email object within this array may contain the recipient’s name, but must always contain the recipient’s email. + /// Thrown when the emails parameter is null + /// Thrown when the emails parameter is empty + public void AddReplyTos(List emails) + { + if (emails == null) + throw new ArgumentNullException("emails"); + if (emails.Count == 0) + throw new InvalidOperationException("Sequence contains no elements"); + + if (ReplyTos == null) ReplyTos = new List(); + ReplyTos.AddRange(emails); + } + /// /// Add a substitution to the email. /// You may not include more than 100 substitutions per personalization object, and the total collective size of your substitutions may not exceed 10,000 bytes per personalization object. diff --git a/tests/SendGrid.Tests/Integration.cs b/tests/SendGrid.Tests/Integration.cs index 7bac0db9b..4ea870598 100644 --- a/tests/SendGrid.Tests/Integration.cs +++ b/tests/SendGrid.Tests/Integration.cs @@ -2217,6 +2217,23 @@ public void TestSetReplyTo() Assert.Equal("{\"reply_to\":{\"name\":\"Test User2\",\"email\":\"test2@example.com\"}}", msg.Serialize()); } + [Fact] + public void TestAddReplyTos() + { + var msg = new SendGridMessage(); + msg.AddReplyTo("test2@example.com", "Test User2"); + Assert.Equal("{\"reply_to_list\":[{\"name\":\"Test User2\",\"email\":\"test2@example.com\"}]}", msg.Serialize()); + + msg = new SendGridMessage(); + msg.AddReplyTo("test1@example.com", "Test User1"); + msg.AddReplyTo("test2@example.com", "Test User2"); + Assert.Equal("{\"reply_to_list\":[{\"name\":\"Test User1\",\"email\":\"test1@example.com\"},{\"name\":\"Test User2\",\"email\":\"test2@example.com\"}]}", msg.Serialize()); + + msg = new SendGridMessage(); + msg.AddReplyTos(new List { new EmailAddress() { Email = "test1@example.com" }, new EmailAddress() { Email = "test2@example.com", Name = "Test User2" } }); + Assert.Equal("{\"reply_to_list\":[{\"email\":\"test1@example.com\"},{\"name\":\"Test User2\",\"email\":\"test2@example.com\"}]}", msg.Serialize()); + } + [Fact] public void TestSetGlobalSubject() { diff --git a/tests/SendGrid.Tests/PreSendEmailValidation/WhenCreatingASendGridMessage.cs b/tests/SendGrid.Tests/PreSendEmailValidation/WhenCreatingASendGridMessage.cs index ad590c047..87f4477c7 100644 --- a/tests/SendGrid.Tests/PreSendEmailValidation/WhenCreatingASendGridMessage.cs +++ b/tests/SendGrid.Tests/PreSendEmailValidation/WhenCreatingASendGridMessage.cs @@ -48,5 +48,19 @@ public void WithANullCarbonCopyThenAnExceptionIsThrown() var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty); Assert.Throws(() => { sendGridMessage.AddCc(null, 0); }); } + + [Fact] + public void WithAnEmptyListOfReplyTosThenAnExceptionIsThrown() + { + var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty); + Assert.Throws(() => { sendGridMessage.AddReplyTos(new List()); }); + } + + [Fact] + public void WithANullListOfReplyTosThenAnExceptionIsThrown() + { + var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty); + Assert.Throws(() => { sendGridMessage.AddReplyTos(null); }); + } } } \ No newline at end of file From f266b0bb95df83a130a60d1a93616efe7c2f604a Mon Sep 17 00:00:00 2001 From: Ryan Fleming Date: Wed, 23 Mar 2022 09:21:39 -0400 Subject: [PATCH 2/2] Fix reference type issue --- src/SendGrid/Helpers/Mail/SendGridMessage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SendGrid/Helpers/Mail/SendGridMessage.cs b/src/SendGrid/Helpers/Mail/SendGridMessage.cs index 01eab4d41..4c62a01f3 100644 --- a/src/SendGrid/Helpers/Mail/SendGridMessage.cs +++ b/src/SendGrid/Helpers/Mail/SendGridMessage.cs @@ -141,7 +141,7 @@ public class SendGridMessage /// /// Gets or sets a list of objects of email objects containing the email address and name of the individuals who should receive responses to your email. /// - [JsonProperty(PropertyName = "reply_to_list")] + [JsonProperty(PropertyName = "reply_to_list", IsReference = false)] public List ReplyTos { get; set; } ///