C # - Find all email addresses for an Active Directory user

I am trying to get all the email addresses associated with this AD user.

For the user, I have a domain and login (for example, DOMAIN \ username), and I AD keep the email addresses in:

  • Mail attribute.
  • In the attributes of proxyAddresses .

So far, I don’t know which C # API to use to connect to AD and how to properly filter the user to get all email addresses. I am using .NET 3.5.

Thanks.

+6
c # active-directory
source share
2 answers

Here a solution is possible using various classes in the System.DirectoryServices namespace.

 string username = "username"; string domain = "domain"; List<string> emailAddresses = new List<string>(); PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain); UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, username); // Add the "mail" entry emailAddresses.Add(user.EmailAddress); // Add the "proxyaddresses" entries. PropertyCollection properties = ((DirectoryEntry)user.GetUnderlyingObject()).Properties; foreach (object property in properties["proxyaddresses"]) { emailAddresses.Add(property.ToString()); } 
+30
source share

You looked at the DirectoryEntry class. You can pull properties if you have the LDAP string set. Is mail authentication ironic?

-one
source share

All Articles