I want to send a single message to multiple recipients (such that they can see that they are recipients). But when I use MailHelper.CreateSingleEmailToMultipleRecipients to construct a message for sending, each recipient receives their own email, with only one recipient on the email. For instance, this means that reply-all does not include the other recipients.
The workaround I've found for this is to make the first recipient the to, and put the rest on the CC list. But it would be nice to know if it's possible to send one email to multiple to recipients.
This is basically the code I'm using:
C#
public async bool SendEmail(List<string> recipients, string subject, string bodyHtml, string sender)
{
var apiKey = "...";
var client = new SendGridClient(apiKey);
var from = MailHelper.StringToEmailAddress(sender);
var tos = recipients.Select(x => MailHelper.StringToEmailAddress(x)).ToList();
var msg = MailHelper.CreateSingleEmail(from, tos, subject, bodyHtml, bodyHtml);
return await client.SendEmailAsync(msg);
}
When I use this, each recipient receives an email and does not see any trace of the other recipients.
You can achieve that without using MailHelper class like in the below code.
public async bool SendEmail(List<string> recipients, string subject, string bodyHtml, string sender)
{
var apiKey = "...";
var client = new SendGridClient(apiKey);
var from = MailHelper.StringToEmailAddress(sender);
var tos = recipients.Select(x => MailHelper.StringToEmailAddress(x)).ToList();
var msg = new SendGridMessage();
msg.SetFrom(from);
msg.SetGlobalSubject(subject);
//use htmlcontent only if you are sending only HTML
if (!string.IsNullOrEmpty(bodyHtml))
{
msg.AddContent(MimeType.Html, bodyHtml);
}
//The AddTos method below accepts List<EmailAddress> to add multiple recipients with Personalization
msg.AddTos(tos);
return await client.SendEmailAsync(msg);
}
let me know if it works.
In the MailHelper class method the emails are added like below
for (var i = 0; i < tos.Count; i++)
{
msg.AddTo(tos[i], i);
}
Hi @thenovices,
Please try the sample code provided by @Niladri24dutta. For a full example, please see here.
Thanks for jumping in with the answer @Niladri24dutta!
Hi @thinkingserious If the above code works then let me know if we can add a new optional Boolean parameter(e.g showAllRecipients) in the CreateSingleEmailToMultipleRecipients method of MailHelper class so that the users can toggle display of all the recipients in the mail according to the requirement. Depending on the boolean value we can use either msg.AddTo() with personalization index or simply msg.AddTos().
Hi @Niladri24dutta,
I like your idea. Here are my observations:
To implement this, we just need to not loop here.
We can create a new method, same name but with a different signature (just adding the showAllRecipients parameter, that when passed False for showAllReceipients we simply call the current CreateSingleEmailToMultipleRecipients method otherwise we build the SendGridMessage object using only one Personalization as described in 1.)
Thoughts?
Thanks!
Elmer
@thinkingserious yes I also like idea of writing an overload for CreateSingleEmailToMultipleRecipients
Even though I have not tested it, but let me know if you were asking about something like below
public static SendGridMessage CreateSingleEmailToMultipleRecipients(
EmailAddress from,
List<EmailAddress> tos,
string subject,
string plainTextContent,
string htmlContent,
bool showAllRecipients)
{
var msg = new SendGridMessage();
if(showAllRecipients)
{
msg.SetFrom(from);
msg.SetGlobalSubject(subject);
if (!string.IsNullOrEmpty(plainTextContent))
{
msg.AddContent(MimeType.Text, plainTextContent);
}
if (!string.IsNullOrEmpty(htmlContent))
{
msg.AddContent(MimeType.Html, htmlContent);
}
msg.AddTos(tos);
}
else
{
msg = CreateSingleEmailToMultipleRecipients(from,tos,subject,plainTextContent,htmlContent);
}
return msg;
}
`
Hi @Niladri24dutta,
Yes, you got it! Thanks!
@thinkingserious let me know if a PR is required for this.
@Niladri24dutta,
A PR would get it to the top of our backlog, otherwise, it may be some time before we can implement. Thanks!
With Best Regards,
Elmer
@thinkingserious ok let me see if I can implement and create a PR for this. will let you know.
@Niladri24dutta @thinkingserious Thanks you so much for the quick response and answer! You guys are awesome! Feel free to close the issue.
Most helpful comment
You can achieve that without using MailHelper class like in the below code.
let me know if it works.
In the MailHelper class method the emails are added like below