locked
Best method to remove duplicate users after To / CC / Bcc have been set RRS feed

  • Question

  • User-1188570427 posted

    I am working with SendGrid.

    Below is the code that we use to set the To/CC/BCC.

    Send Grid has a requirement where an email address can only live in one (To/CC/BCC) and not multiple ones.

    If the email address is located in more than one, then you will receive this error:

    Received status code: BadRequest - Errors: {"errors":[{"message":"Each email address in the personalization block should be unique between to, cc, and bcc. We found the first duplicate instance of [myemail@mycompany.com] in the personalizations.0.cc

    What would be the best way to validate / confirm after setting the list that an email address is ONLY in one of the To/CC/BCC?

                    foreach (var address in personalization.ToAddresses)
                    {
                        sendGridPersonalization.Tos.Add(new EmailAddress { Email = address.Email, Name = address.Name });
                    }
    
                    if (personalization.CCAddresses != null)
                    {
                        sendGridPersonalization.Ccs = new List<EmailAddress>();
                        foreach (var address in personalization.CCAddresses)
                        {
                            sendGridPersonalization.Ccs.Add(new EmailAddress { Email = address.Email, Name = address.Name });
                        }
                    }
    
                    if (personalization.BCCAddresses != null)
                    {
                        sendGridPersonalization.Bccs = new List<EmailAddress>();
                        foreach (var address in personalization.BCCAddresses)
                        {
                            sendGridPersonalization.Bccs.Add(new EmailAddress { Email = address.Email, Name = address.Name });
                        }
                    }

    Thursday, May 16, 2019 8:22 PM

All replies

  • User-1174608757 posted

    Hi tvb2727,

     According to your description, if you want to use SendGrid for multiply recipients , you need to add the Personalizations list for that. This class is designed to be sent to multiple users. Here is the code , I hope it could help you.

    var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
    var client = new SendGridClient(apiKey);
    var msg = new SendGridMessage()
    {
        From = new EmailAddress("sender@email.com", "Sender Name"),
        Subject = "Subject",
        PlainTextContent = "Text for body",
        HtmlContent = "<strong>Hello World!",
        Personalizations = new List<Personalization>
        {
             new Personalization
             {
                  Tos = new List<EmailAddress> 
                  {
                       new EmailAddress("abc@email.com", "abc"),
                       new EmailAddress("efg@email.com", "efg")
                  }
             }
         }
    };
    
    var response = await client.SendEmailAsync(msg);

    Best Regards

    Wei

    Friday, May 17, 2019 2:09 AM
  • User-1188570427 posted

    Hi tvb2727,

     According to your description, if you want to use SendGrid for multiply recipients , you need to add the Personalizations list for that. This class is designed to be sent to multiple users. Here is the code , I hope it could help you.

    var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
    var client = new SendGridClient(apiKey);
    var msg = new SendGridMessage()
    {
        From = new EmailAddress("sender@email.com", "Sender Name"),
        Subject = "Subject",
        PlainTextContent = "Text for body",
        HtmlContent = "<strong>Hello World!",
        Personalizations = new List<Personalization>
        {
             new Personalization
             {
                  Tos = new List<EmailAddress> 
                  {
                       new EmailAddress("abc@email.com", "abc"),
                       new EmailAddress("efg@email.com", "efg")
                  }
             }
         }
    };
    
    var response = await client.SendEmailAsync(msg);

    Best Regards

    Wei

    I know how to send the email.

    The issue is: we have email addresses that could land in the To/CC/Bcc, but they can ONLY be in one of them per SendGrids's rules.

    I am trying to find a way to quickly and efficiently make sure that it is ONLY in one. 

    Example, if it is in the To, then it needs to be removed from the CC.

    I'm trying to get some ideas!

    Friday, May 17, 2019 3:46 AM
  • User-893317190 posted

    Hi tvb2727,

    When you want to get elements that are not in another list from a list. You could try linq's except method, it could remove elements that is in another list from one list and return the list which is not duplicate.

    Below is a small sample.

     protected void Page_Load(object sender, EventArgs e)
            {
                Personalization personalization =
    
              new Personalization
              {
                  Tos = new List<EmailAddress>
                   {
                       new EmailAddress("abc@email.com", "abc"),
                       new EmailAddress("efg@email.com", "efg")
                   },
                  Ccs = new List<EmailAddress>
                  {
                      new EmailAddress("abc@email.com", "abc"),
                      new EmailAddress("efg@email.com", "efg"),
                         new EmailAddress("hij@email.com", "hij"),
                  }
                  ,
                  Bccs = new List<EmailAddress>
                  {
                      new EmailAddress("abc@email.com", "abc"),
                      new EmailAddress("efg@email.com", "efg"),
                         new EmailAddress("hij@email.com", "hij"),
                         new EmailAddress ("klm@email.com","klm")
                  }
              };
                EmailCompatator emailCompatator = new EmailCompatator();
                personalization.Bccs = personalization.Bccs.Except(personalization.Ccs, emailCompatator).ToList(); // remove all duplicate emails from Bccs
                personalization.Ccs = personalization.Ccs.Except(personalization.Tos, emailCompatator).ToList();  // remove all duplicate emails from ccs
    
                GridView1.DataSource = personalization.Tos;
                GridView1.DataBind();
    
                GridView2.DataSource = personalization.Ccs;
                GridView2.DataBind();
    
                GridView3.DataSource = personalization.Bccs;
                GridView3.DataBind();
    
            }
    
        }
        public class EmailCompatator : IEqualityComparer<EmailAddress>
        {
            public bool Equals(EmailAddress x, EmailAddress y)
            {
                return x.Email == y.Email;
            }
    
            public int GetHashCode(EmailAddress obj)
            {
                return 1;
            }
        }

    To use except method , you should provide a class implementing IEqualityComparer , which is used to tell the except method whether two emailaddresses are the same.

    Here I assume that if two email addresses have the same email, they are the same.

    You could customize the rule.

    The GetHashCode method should return the same value , because except method will also compare hashcode, if  two emails have different hashcode, they will also be treated as different.

    The result.

    Best regards,

    Ackerly Xu

    Friday, May 17, 2019 6:20 AM
  • User-1188570427 posted

    Hi tvb2727,

    When you want to get elements that are not in another list from a list. You could try linq's except method, it could remove elements that is in another list from one list and return the list which is not duplicate.

    Below is a small sample.

     protected void Page_Load(object sender, EventArgs e)
            {
                Personalization personalization =
    
              new Personalization
              {
                  Tos = new List<EmailAddress>
                   {
                       new EmailAddress("abc@email.com", "abc"),
                       new EmailAddress("efg@email.com", "efg")
                   },
                  Ccs = new List<EmailAddress>
                  {
                      new EmailAddress("abc@email.com", "abc"),
                      new EmailAddress("efg@email.com", "efg"),
                         new EmailAddress("hij@email.com", "hij"),
                  }
                  ,
                  Bccs = new List<EmailAddress>
                  {
                      new EmailAddress("abc@email.com", "abc"),
                      new EmailAddress("efg@email.com", "efg"),
                         new EmailAddress("hij@email.com", "hij"),
                         new EmailAddress ("klm@email.com","klm")
                  }
              };
                EmailCompatator emailCompatator = new EmailCompatator();
                personalization.Bccs = personalization.Bccs.Except(personalization.Ccs, emailCompatator).ToList(); // remove all duplicate emails from Bccs
                personalization.Ccs = personalization.Ccs.Except(personalization.Tos, emailCompatator).ToList();  // remove all duplicate emails from ccs
    
                GridView1.DataSource = personalization.Tos;
                GridView1.DataBind();
    
                GridView2.DataSource = personalization.Ccs;
                GridView2.DataBind();
    
                GridView3.DataSource = personalization.Bccs;
                GridView3.DataBind();
    
            }
    
        }
        public class EmailCompatator : IEqualityComparer<EmailAddress>
        {
            public bool Equals(EmailAddress x, EmailAddress y)
            {
                return x.Email == y.Email;
            }
    
            public int GetHashCode(EmailAddress obj)
            {
                return 1;
            }
        }

    To use except method , you should provide a class implementing IEqualityComparer , which is used to tell the except method whether two emailaddresses are the same.

    Here I assume that if two email addresses have the same email, they are the same.

    You could customize the rule.

    The GetHashCode method should return the same value , because except method will also compare hashcode, if  two emails have different hashcode, they will also be treated as different.

    The result.

    Best regards,

    Ackerly Xu

    I think this is the right direction I need. I still need to test. 

    I see how you take care of the Bccs first and then update those from the Ccs and check the Ccs against the Tos.

    Friday, May 17, 2019 11:10 PM