How to find exchange server users?
DirectoryEntry entry = new DirectoryEntry(_ldap, userName, password); // _ldap is domain such as "LDAP://xxx.xxx.com";
DirectorySearcher ds = new DirectorySearcher(entry);
ds.Filter = String.Format("(&(objectCategory=person)(objectClass=user)(|(displayname={0}*)(lastname={0}*)(mail={0}*)))", keyword); // search by these fields
ds.PropertiesToLoad.AddRange(new string[] { "samaccountname", "displayname", "mail", "l" });
ds.SizeLimit = 50; // result number returned
SearchResultCollection results = ds.FindAll();
foreach (SearchResult result in results)
if (resultProperties.Contains("mail"))
name = resultProperties["mail"][0].ToString();
// Should use "Contains" check first to improve performance, otherwise if use resultProperties["mail"] directly, when resultProperties doesn'thas field "mail", performance is bad.
}