+2 votes

hi,
first thanks for your great mail.dll product. it is easy use.
i have a problem that i need to check wheather given address in to or cc address collection (with out iterating collection like using msginfo.envelop.To.contains(test@gmail.com)). could you please help me

by
edited

1 Answer

0 votes
 
Best answer

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

by (301k points)
...