1: public static string GetUsersEmail(string User, string UserName, string Password)
2: {
3: string EmailAddress = "";
4: DirectoryEntry Entry = new DirectoryEntry("LDAP://ADserver", //Make sure to point this at your AD server
5: UserName,
6: Password,
7: AuthenticationTypes.Secure);
8: DirectorySearcher Searcher = new DirectorySearcher(Entry);
9: Searcher.PropertiesToLoad.Add("mail");
10: string Filter = String.Format("(&(objectcategory=user)(cn=" + User + "))");
11:
12: Searcher.Filter = Filter;
13: Searcher.Sort.Direction = SortDirection.Ascending;
14: Searcher.Sort.PropertyName = "cn";
15:
16: SearchResultCollection Results;
17:
18: Results = Searcher.FindAll();
19:
20: foreach (SearchResult Result in Results)
21: {
22: ResultPropertyCollection Properties = Result.Properties;
23: foreach (string Key in Properties.PropertyNames)
24: {
25: if (Key == "mail")
26: {
27: foreach (object Values in Properties[Key])
28: {
29: EmailAddress = Values.ToString();
30: break;
31: }
32: }
33: }
34: }
35: return EmailAddress;
36: }