I'm looping through all the users in our AD environment, and reading their email address so I can update it in another system. I'm using the System.DirectoryServices
library. Simplified example below. What I'm finding is that the mail
attribute is not always correct and is often missing even though the user has a valid mailbox setup and functional in Exchange. So, the question is, given a DirectoryEntry
, is there any way to list the user's primary email address (and ideally any aliases) from Exchange other than reading the mail
attribute? I've seen some examples that use the proxyAddresses
attribute, but these don't seem to be reliable either. I am looking for a solution that comes directly from Exchange. No if-and-or-buts about it. This is their email address.
DirectorySearcher search = new DirectorySearcher("(&(mail=*))");
search.PageSize = 1000;
search.PropertiesToLoad.Add("mail");
foreach( SearchResult result in search.FindAll() )
{
DirectoryEntry entry = result.GetDirectoryEntry();
Console.WriteLine(entry.Properties["mail"].Value);
}
Exchange doesn't have a separate data store for this data. It's stored in AD. If you collect the data via an Exchange API (EWS or PowerShell), you're ultimately getting the same data. The proxyAddresses
attribute is the place to look in AD.