如何使用C#从Active Directory获取所有电子邮件列表?

时间:2019-04-21 06:48:24

标签: c# email active-directory

我想从Active Directory中获取所有电子邮件地址。在下面,我尝试了这种情况,但没有收到所有电子邮件。我需要所有电子邮件,例如ObjectClass=groupObjectClass=user

我尝试过这些,

deSearch.Filter = "(|(ObjectClass=user)(ObjectClass=group))";

deSearch.Filter = "(mail=*@abc.com)";

完整代码:

DirectoryEntry de = new DirectoryEntry();
de.AuthenticationType = AuthenticationTypes.Secure;

DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(mail=*)";

SearchResultCollection results = deSearch.FindAll();

foreach (SearchResult sResultSet in results)
{
    if (sResultSet.Properties["Mail"].Count > 0) 
    {
        if (sResultSet.Properties.Contains("Mail")
        {
            mailPackage.Mail = sResultSet.Properties["Mail"][0].ToString();
        }
        mailList.Add(mailPackage);
    }
}
return mailList;

0 个答案:

没有答案