简体   繁体   English

使用Active Directory C#从组中获取所有用户的电子邮件

[英]Get all users email from a group using Active Directory c#

I need all email address of a particular group. 我需要特定组的所有电子邮件地址。 Please help me 请帮我

string filter1 = string.Format("(&(objectClass=group)(cn={0}))", "groupname");
        DirectorySearcher searcher = new DirectorySearcher(entry);
        searcher.Filter = filter1;
        searcher.SearchScope = SearchScope.Subtree;
        searcher.PropertiesToLoad.Add("member");
        SearchResult res = searcher.FindOne();
        ArrayList userNames = new ArrayList();
        if (res != null)
        {
            for (int counter = 0; counter <res.Properties["member"].Count; counter++)
            {
                string user = (string)res.Properties["member"][counter];
                userNames.Add(user);
            }
        }

I am getting uesr name and other details but not get email. 我正在获得用户名称和其他详细信息,但没有收到电子邮件。 Please tell me the way to find email address of each user directly. 请告诉我直接查找每个用户的电子邮件地址的方法。

You can try with this code - based on PrincipalContext class 您可以尝试使用此代码-基于PrincipalContext

var username = "username";
var domain = "domain";
var emailAddresses = new List<string>();

var principalContext = new PrincipalContext(ContextType.Domain, domain);
var userPrincipal  = UserPrincipal.FindByIdentity(principalContext, username);
// Add the "mail" entry
emailAddresses.Add(userPrincipal.EmailAddress);

Link : http://msdn.microsoft.com/fr-fr/library/bb344891(v=vs.90).aspx 链接: http : //msdn.microsoft.com/fr-fr/library/bb344891(v=vs.90).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM