Skip to content

Commit c3bc286

Browse files
Merge pull request #508 from Niladri24dutta/SingleEmailToMultipleOverload
Single email to multiple recipients - Toggle display of recipients
2 parents 45b79a5 + 7de2308 commit c3bc286

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/SendGrid/Helpers/Mail/MailHelper.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,48 @@ public static EmailAddress StringToEmailAddress(string rfc2822Email)
146146
var name = match.Groups[NameGroup].Value.Trim();
147147
return new EmailAddress(email, name);
148148
}
149+
150+
/// <summary>
151+
/// Send a single simple email to multiple recipients with option for displaying all the recipients present in "To" section of email
152+
/// </summary>
153+
/// <param name="from">An email object that may contain the recipient’s name, but must always contain the sender’s email.</param>
154+
/// <param name="tos">A list of email objects that may contain the recipient’s name, but must always contain the recipient’s email.</param>
155+
/// <param name="subject">The subject of your email. This may be overridden by SetGlobalSubject().</param>
156+
/// <param name="plainTextContent">The text/plain content of the email body.</param>
157+
/// <param name="htmlContent">The text/html content of the email body.</param>
158+
/// <param name="showAllRecipients">Displays all the recipients present in the "To" section of email.The default value is false</param>
159+
/// <returns>A SendGridMessage object.</returns>
160+
public static SendGridMessage CreateSingleEmailToMultipleRecipients(
161+
EmailAddress from,
162+
List<EmailAddress> tos,
163+
string subject,
164+
string plainTextContent,
165+
string htmlContent,
166+
bool showAllRecipients = false)
167+
{
168+
var msg = new SendGridMessage();
169+
if (showAllRecipients)
170+
{
171+
msg.SetFrom(from);
172+
msg.SetGlobalSubject(subject);
173+
if (!string.IsNullOrEmpty(plainTextContent))
174+
{
175+
msg.AddContent(MimeType.Text, plainTextContent);
176+
}
177+
178+
if (!string.IsNullOrEmpty(htmlContent))
179+
{
180+
msg.AddContent(MimeType.Html, htmlContent);
181+
}
182+
183+
msg.AddTos(tos);
184+
}
185+
else
186+
{
187+
msg = CreateSingleEmailToMultipleRecipients(from, tos, subject, plainTextContent, htmlContent);
188+
}
189+
190+
return msg;
191+
}
149192
}
150193
}

tests/SendGrid.Tests/Integration.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,65 @@ public void TestCreateSingleEmailToMultipleRecipients()
448448
Assert.True(msg5.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}");
449449
}
450450

451+
[Fact]
452+
public void TestCreateSingleEmailToMultipleRecipientsToggleRecipientDisplay()
453+
{
454+
var emails = new List<EmailAddress>
455+
{
456+
new EmailAddress("[email protected]"),
457+
new EmailAddress("[email protected]"),
458+
new EmailAddress("[email protected]")
459+
};
460+
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
461+
emails,
462+
"Test Subject",
463+
"Plain Text Content",
464+
"HTML Content"
465+
);
466+
Assert.True(msg.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"},{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}");
467+
468+
var msg2 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
469+
emails,
470+
"Test Subject",
471+
null,
472+
"HTML Content"
473+
);
474+
Assert.True(msg2.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}");
475+
476+
var msg3 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
477+
emails,
478+
"Test Subject",
479+
"Plain Text Content",
480+
null
481+
);
482+
Assert.True(msg3.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}");
483+
484+
var msg4 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
485+
emails,
486+
"Test Subject",
487+
"",
488+
"HTML Content"
489+
);
490+
Assert.True(msg4.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}");
491+
492+
var msg5 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
493+
emails,
494+
"Test Subject",
495+
"Plain Text Content",
496+
""
497+
);
498+
Assert.True(msg5.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}");
499+
500+
var msg6 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
501+
emails,
502+
"Test Subject",
503+
"Plain Text Content",
504+
"HTML Content",
505+
true
506+
);
507+
Assert.True(msg6.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"},{\"email\":\"[email protected]\"},{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"},{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}");
508+
}
509+
451510
[Fact]
452511
public void TestCreateMultipleEmailsToMultipleRecipients()
453512
{

0 commit comments

Comments
 (0)