【发布时间】:2017-01-19 22:19:40
【问题描述】:
我正在尝试获取我们公司域中所有用户的电子邮件地址。 99% 的工作,但有时我的输出中有 y NullReferenceException。
代码:
string dom = "mydomain";
System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry("LDAP://" + dom); //domain, user, password
System.DirectoryServices.DirectorySearcher ds = new System.DirectoryServices.DirectorySearcher(entry);
ds.Filter = ("(objectClass=User)");
int count = 1;
foreach (System.DirectoryServices.SearchResult resEnt in ds.FindAll())
{
try
{
System.DirectoryServices.DirectoryEntry de = resEnt.GetDirectoryEntry();
String email = de.Properties["mail"].Value.ToString();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
【问题讨论】:
-
我实际上没有在您的帖子中看到任何问题。你有什么问题?
-
如果有人知道为什么
codeString email = de.Properties["mail"].Value.ToString();code -
//编辑:我收到 377 个电子邮件地址,但在我们的域中有 383...不能为这 6 个用户工作?
-
您是否尝试过使用AD Explorer 之类的工具实际查看这些用户并检查他们的属性以确保他们确实拥有与其他人一样的邮件属性?
-
抱歉,有些用户没有电子邮件地址
标签: c# .net windows exception active-directory