read email from active directory
private static string GetEmailAddressLDAP(string strEnv, string empno)
{
string strLDAPConnection;
string strLDAPId;
string strLDAPPwd;
if (strEnv.IndexOf("P") == 0)
{
//'PROD
strLDAPConnection = "LDAP://xxxx/ou=xxxx,dc=xx,dc=xxx";
strLDAPId = @"xxxxxxxxxx";
strLDAPPwd = "xxxxxxx";
}
DirectoryEntry entry = new DirectoryEntry(strLDAPConnection, strLDAPId, strLDAPPwd);
DirectorySearcher search = new DirectorySearcher(entry);
char pad = '0';
search.Filter = String.Format("(sAMAccountName=*{0})", empno.PadLeft(6, pad).Substring(empno.PadLeft(6, pad).Length - 6));
SearchResult result = search.FindOne();
if (result != null)
{
DirectoryEntry currentUser = result.GetDirectoryEntry();
if (currentUser.Properties["mail"].Value != null)
{
string sEmail = currentUser.Properties["mail"].Value.ToString();
return sEmail;
}
else
{
return "email not found";
}
}
else
{
return "employee NOT FOUND";
}
}