Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I send to multiple email addresses without all emails being seen in the "To" section of each email?

I can send emails to multiple people with SendGrid in .Net, that’s not the issue. It’s when an email recipient gets his email he can see all other email addresses from all the other recipients.

QUESTION – How do I send all my emails out to the recipients from a list (array), but not show all other recipients?

I tried changing msg.AddTos() to msg.AddBccs() but that didn’t seem to even send out the emails at all, or at least I didn’t get them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Ex. Here is what the recipient see’s when he opens his email. He can see his email "Test Roy" and all other email recipients.

enter image description here

Here is my function that send the email.

private async Task < Response > ExecuteTemplate(string[] toEmailAddresses, string subject, dynamic templateData) {
  var client = new SendGridClient(sendGridOptions.SendGridKey);
  var msg = new SendGridMessage();
  msg.SetFrom(new EmailAddress("automated@mysite.com", "MySite"));
  msg.SetTemplateId(_config["SendGrid:TemplateId"]);
  templateData.subject = subject;
  msg.SetTemplateData(templateData);

  if (toEmailAddresses.Length > 1) {
    var emailAddresses = toEmailAddresses.Select(array => new EmailAddress(array)).ToList();
    // msg.AddBccs(emailAddresses);
    msg.AddTos(emailAddresses);
  } else {
    msg.AddTo(toEmailAddresses[0]);
  }

  msg.SetClickTracking(false, false);
  msg.SetOpenTracking(false);
  msg.SetGoogleAnalytics(false);
  msg.SetSubscriptionTracking(false);

  var response = await client.SendEmailAsync(msg);

  return response;
}

>Solution :

C# SendGrid supports mailing which shows all recipients in the email via
CreateSingleEmailToMultipleRecipients (L269 – L275)

public static SendGridMessage CreateSingleEmailToMultipleRecipients(
    EmailAddress from,
    List<EmailAddress> tos,
    string subject,
    string plainTextContent,
    string htmlContent,
    bool showAllRecipients = false)

By default, the showAllRecipients parameter is set to false. If you compare the code between the function above when providing showAllRecipients = true and L97 – L102:

L292

msg.AddTos(tos);

L119

msg.AddTo(tos[i], i);

This is the difference of not showing all the (To) recipients in the email.

Thus, as you are using the CreateSingleEmailToMultipleRecipients method, this will create the email message without showing all recipients.

private async Task<Response> ExecuteTemplate(string[] toEmailAddresses, string subject, dynamic templateData) 
{
    templateData.subject = subject;

    var client = new SendGridClient(sendGridOptions.SendGridKey);
    var msg = MailHelper.CreateSingleTemplateEmailToMultipleRecipients(
        new EmailAddress("automated@mysite.com", "MySite"),
        toEmailAddresses.Select(array => new EmailAddress(array)).ToList(),
        _config["SendGrid:TemplateId"].ToString(),
        templateData
    );

  msg.SetClickTracking(false, false);
  msg.SetOpenTracking(false);
  msg.SetGoogleAnalytics(false);
  msg.SetSubscriptionTracking(false);

  var response = await client.SendEmailAsync(msg);

  return response;
}

Otherwise, you can try maintaining your existing code, but making changes here:

if (toEmailAddresses.Length > 1) 
{
    for (int i = 0; i < toEmailAddresses.Count(); i++)
    {
        msg.AddTos(new EmailAddress(toEmailAddresses[i]), i);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading