我正在尝试从公司的AD系统中检索所有电子邮件地址及其邮件地址。我有大约1800个团体,但我发现有大约20个gourps,我无法获得他们的属性。我尝试了我的Outlook并正确地获得了邮件地址等属性。但我无法通过代码获取它们,有人请求帮助。谢谢。以下是我的代码段:
static void TestGroupEmails()
{
ICollection<DirectoryEntry> groups = GetGroups();
Console.WriteLine(groups.Count + "groups");
List<String> noNameGroups = new List<String>();
foreach (DirectoryEntry de in groups)
{
String name = de.Properties["sAMAccountName"].Value as String;
String email = de.Properties["mail"].Value as String;
if (String.IsNullOrEmpty(email))
noNameGroups.Add(name);
}
StreamWriter writer = new StreamWriter(@"C:\ad\group mails.txt");
noNameGroups.Sort();
foreach (String name in noNameGroups)
{
writer.WriteLine(name);
}
writer.Close();
Console.ReadLine();
}
public static List<DirectoryEntry> GetGroups()
{
String filter = @"(&(objectCategory=group))";
List<DirectoryEntry> groups = new List<DirectoryEntry>();
using (DirectoryEntry root = new DirectoryEntry(Constants.ADConnPrefix))
{
using (DirectorySearcher searcher = new DirectorySearcher(filter, null))
{
searcher.PageSize = 10000;
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.SearchScope = SearchScope.Subtree;
searcher.SearchRoot = root;
root.Username = Constants.UserName;
root.Password = Constants.Password;
using (SearchResultCollection searchResult = searcher.FindAll())
{
foreach (SearchResult sr in searchResult)
{
DirectoryEntry de = sr.GetDirectoryEntry();
groups.Add(de);
}
}
}
}
return groups;
}
public static SearchResult GetGroupInfo(String groupName)
{
String normalName = Utility.RemoveLoginNamePrefix(groupName);
String filterFormat = "(&(objectCategory=group)(sAMAccountName={0}))";
using (SearchResultCollection searchResult = Search(ADConnPrefix, null, filterFormat, normalName))
{
int count = searchResult.Count;
SearchResult sr = searchResult[0];
return sr;
}
}
答案 0 :(得分:1)
您确定有问题的群组实际上有电子邮件地址吗? AD中的群体可能没有它们。
如果您将搜索过滤器修改为(&(objectCategory=group)(mail=*))
,则会过滤掉所有没有电子邮件地址的群组。