Reputation: 7989
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);
}
Upvotes: 0
Views: 2441
Reputation: 4503
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.
Upvotes: 1
Reputation: 2327
there is a simple way to know which property is used in the AD for the active directory properties though it is a bit manual work to do but after going through some samples you will come to know all the properties that being used to hold the email value
below, you can loop over the properties names for some user and try to notice which property holds the email value after several tries with different users you will be able to know the different properties name that hold the email and used for getting the email from all over the AD
DirectoryEntry directoryEntry = new DirectoryEntry(ConnectionString, ProviderUserName, ProviderPassword, AuthenticationTypes.Secure);
/******************************/
DirectorySearcher search = new DirectorySearcher(directoryEntry);
search.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
search.CacheResults = false;
SearchResultCollection allResults = search.FindAll();
StringBuilder sb = new StringBuilder();
foreach (SearchResult searchResult in allResults)
{
foreach (string propName in searchResult.Properties.PropertyNames)
{
ResultPropertyValueCollection valueCollection = searchResult.Properties[propName];
foreach (Object propertyValue in valueCollection)
{
sb.AppendLine(string.Format("property:{0}, value{1}", propName, propertyValue));
}
}
}
return sb.ToString();
you can even use this to get some properties name that you can't find directly in the AD
Upvotes: 0
Reputation: 1022
Exchange holds the email and aliases. loop through your AD and use Microsoft.Exchange.WebServices.dll to find the user in exchange. then do what you'd like with their email addresses and aliases :)
Upvotes: 0
Reputation: 1677
According to this
it depends on what properties are set when the object is created and what type of object it is:
For mailboxes (objectClass=user) if any of proxyAddress, mail, or textEncodedOrAccess is set exchange will not set any if the values, if none are set exchange will set all of the values.
For Mail-Enabled Objects (objectClass=user & objectClass=contact) targetAddress is mandatory, and the same rules apply for the other three.
Upvotes: 0