I need to get the emailid开发者_如何学Python's from outlook in my C# application to send mail. I have userId or FirstName and LastName. Please suggest me a suitable method.
Thanks
public string GetEmaiId(string userId)
{
string email = string.Empty;
DirectorySearcher objsearch = new DirectorySearcher();
string strrootdse = objsearch.SearchRoot.Path;
DirectoryEntry objdirentry = new DirectoryEntry(strrootdse);
objsearch.Filter = "(& (cn=" + userId + ")(objectClass=user))";
objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree;
objsearch.PropertiesToLoad.Add("cn");
objsearch.PropertyNamesOnly = true;
objsearch.Sort.Direction = System.DirectoryServices.SortDirection.Ascending;
objsearch.Sort.PropertyName = "cn";
SearchResultCollection colresults = objsearch.FindAll();
foreach (SearchResult objresult in colresults)
{
email = objresult.GetDirectoryEntry().Properties["mail"].Value.ToString();
}
objsearch.Dispose();
return email;
}
精彩评论