title: Prevent duplicate email addresses in SendGrid message?
body: <p>Using <a href="https://github.com/sendgrid/sendgrid-csharp" rel="nofollow noreferrer">SendGrid for C# library</a> and SendGrid API v3, if you accidentally include the same email address twice, it results in the email not being sent at all.</p>
  
  <p>Per the SendGrid <a href="https://sendgrid.com/docs/for-developers/sending-email/personalizations/" rel="nofollow noreferrer">documentation</a>:</p>
  
  <blockquote>
    <p>All of the recipients in a single personalization object (either in the to, cc, or bcc fields) will see exactly the same email, as defined by the data in that personalization, as such we do not allow duplicate emails between these 3 arrays in a single personalization.</p>
  </blockquote>
  
  <p>For example:</p>
  
  <pre><code>        var msg = new SendGridMessage();
          msg.AddTo("test@example.com");
          msg.AddBcc("test@example.com"); // duplicate email address, nothing will be sent
  </code></pre>
  
  <p>Prior to sending emails, I would like to check the SendGrid <code>Personalizations</code> and remove any duplicate emails.</p>
  
  <p>Here's the code I have so far:</p>
  
  <pre><code>        var msg = new SendGridMessage();
          msg.AddTo("test@example.com");
          msg.AddBcc("test@example.com"); //has a duplicate email address, nothing will be sent
  
          foreach (var p in msg.Personalizations)
          {
              var emails = new List&lt;EmailAddress&gt;();
              emails.AddRange(p.Tos);
              emails.AddRange(p.Ccs);
              emails.AddRange(p.Bccs);
  
              // find duplicates
              var duplicates = emails.GroupBy(x =&gt; x.Email)
                              .Where(g =&gt; g.Count() &gt; 1)
                              .Select(y =&gt; y.Key)
                              .ToList();
  
              // remove all but one...
              // remove duplicates from Bccs first, then Ccs, then Tos
              foreach (var d in duplicates)
              {
  
              }
          }
  </code></pre>
  
  <p>I'm stuck on the best way to reduce the duplicate email addresses down to one unique one. Has anyone found a good way to enforce having no duplicate email addresses with SendGrid? </p>