Search code examples
c#active-directorytraversal

Reading email from AD


I'm trying to get email adress from AD. I Use below code:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.SamAccountName = "XXX";
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
var found = srch.FindOne();

and this code returns this: enter image description here

As You can see using Local window I access and read email but I need to do it for ~50 accounts. But I can not how to access that place by code. What should I Write after found

found.{something here}.EmailAdress

To get it work?


Solution

  • In order to get there I needed to convert found into DirectoryEntry using this code:

    var a = found.GetUnderlyingObject() as DirectoryEntry;
    

    And after that I was able to access mail from list of 71 parametres:

    a.Properties["mail"].Value.ToString()