Here's the code that check if specified email address is present in MailAddress collection:
if (ContainsEmail(email.To, "pat@example.com"))
{
// ...
}
private static bool ContainsEmail(IEnumerable<MailAddress> list, string email)
{
foreach (MailAddress address in list)
{
if (address.GetMailboxes().ConvertAll(x => x.Address).Contains(email))
return true;
}
return false;
}
// Linq version:
private static bool ContainsEmail(IEnumerable<MailAddress> list, string email)
{
return list.Any(address => address.GetMailboxes().ConvertAll(
x => x.Address).Contains(email));
}
Here's an article that explains how to access To, Cc, Bcc email collections:
https://www.limilabs.com/blog/how-to-access-to-cc-bcc-fields