Return to Snippet

Revision: 35513
at November 9, 2010 21:44 by poxet


Initial Code
public static string GetEmailBySAMAccountName(string sAMAccountName)
{
    var oroot = new System.DirectoryServices.DirectoryEntry("LDAP://INVESTOR_AB");
    var osearcher = new System.DirectoryServices.DirectorySearcher(oroot);
    osearcher.Filter = string.Format("(&(sAMAccountName={0}))", sAMAccountName);
    var oresult = osearcher.FindAll();

    if (oresult.Count == 0) throw new InvalidOperationException(string.Format("Cannot find sAMAccountName {0} in LDAP.", sAMAccountName));
    if (oresult.Count > 1) throw new InvalidOperationException(string.Format("There are {0} items with sAMAccountName {1} in LDAP.", oresult.Count, sAMAccountName));

    return oresult[0].Properties["mail"][0].ToString();
}

Initial URL

                                

Initial Description
Uses LDAP to find EMail address by using the sAMAccountName.

Add System.DirectoryServices to references.

Initial Title
Get Email by sAMAccountName from LDAP

Initial Tags
email

Initial Language
C#