Search code examples
c#.netemailsendgrid

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.

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("[email protected]", "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 or CreateSingleTemplateEmailToMultipleRecipients 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("[email protected]", "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);
        }
    }