on this last line i get 'sr.Properties["mail"][0]' threw an exception of type 'System.ArgumentOutOfRangeException'
most of the users in AD have emails, but sme dont. But this exception i get no matter if the user has email account or no. How do i get all the emails of the users?
thanks
Dont forget to click "Mark as Answer" on the post that helped you. Don't "Mark as Answer" if it is not your question.
Can someone please post an answer without posting a link to a link to a link?
foreach( SearchResult sr in myRes)
{
try
{
if ( null != sr.Properties["mail"] && 0 < sr.Properties["mail"].Count )
a = sr.Properties["mail"][0].ToString();
}
catch(Exception ex)
{
// Write whatever you want to check the error, or nothing if you don't.
}
}
Please mark the most helpful reply/replies as "Answer".
RioDD
Member
487 Points
331 Posts
email problem
Oct 26, 2007 10:16 AM|LINK
hello,
I have directory searcher defined like:
DirectoryEntry
entry = new DirectoryEntry("LDAP://DC=domain,DC=local", "userwithadminprivileges", "password", AuthenticationTypes.ReadonlyServer); DirectorySearcher myDir = new DirectorySearcher(entry); String filterString = "(&(objectClass=user)(objectCategory=person)(!UserAccountControl:1.2.840.113556.1.4.803:=2))";myDir.Filter = filterString;
myDir.PropertiesToLoad.Add("mail");SearchResultCollection myRes;myRes = myDir.FindAll();
String a;
foreach (SearchResult sr in myRes){
a=sr.Properties["mail"][0].ToString();
}
on this last line i get 'sr.Properties["mail"][0]' threw an exception of type 'System.ArgumentOutOfRangeException'
most of the users in AD have emails, but sme dont. But this exception i get no matter if the user has email account or no. How do i get all the emails of the users?
thanks
Can someone please post an answer without posting a link to a link to a link?
jonathanpark...
Member
624 Points
103 Posts
Re: email problem
Oct 26, 2007 12:52 PM|LINK
Try this:
foreach (SearchResult sr in myRes)
{
if (sr.Properties["mail"] != null && sr.Properties["mail"].Length > 0)
{
a = sr.Properties["mail"][0].ToString();
}
}
HTH,
Jonathan.
jaloplo
Participant
1012 Points
254 Posts
Re: email problem
Oct 26, 2007 12:53 PM|LINK
Check this:
foreach( SearchResult sr in myRes) { try { if ( null != sr.Properties["mail"] && 0 < sr.Properties["mail"].Count ) a = sr.Properties["mail"][0].ToString(); } catch(Exception ex) { // Write whatever you want to check the error, or nothing if you don't. } }My Blog