获取AD Group User的电子邮件

时间:2022-07-08 03:00:39

How we can retrieve the email id of the user who has belonged to a particular group. I have one application in which i need to send the email to a Security group from sharepoint. I got the list of users by using this code.

我们如何检索属于特定组的用户的电子邮件ID。我有一个应用程序,我需要从sharepoint发送电子邮件到安全组。我使用此代码获得了用户列表。

PrincipalContext principal = new PrincipalContext(ContextType.Domain);
GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principal,  picker.DisplayText);
var members = groupPrincipal.GetMembers(true);

the members variable should returns the group users, So i need to pick each of the user's email id from this collection. Please help me to sort out this issue.

members变量应该返回组用户,所以我需要从这个集合中选择每个用户的电子邮件ID。请帮我解决这个问题。

1 个解决方案

#1


0  

There are a few different ways to do this. Here are two simple options with few lines of code.

有几种不同的方法可以做到这一点。这是两个简单的选项,几行代码。

If your UPNs match your email addresses you could do something like this:

如果您的UPN与您的电子邮件地址匹配,您可以执行以下操作:

var userEmails = new List<string>();
foreach (var member in members)
{
    userEmails.Add(member.UserPrincipalName);
}

If that is not stringent enough and you want to actually pull the email the easiest way is probably to pull the user principal and read it's email property.

如果这不够严格并且您想要实际提取电子邮件,最简单的方法可能是拉取用户主体并读取它的电子邮件属性。

foreach (var member in members)
{
    var user = UserPrincipal.FindByIdentity(context, member.SamAccountName);
    userEmails.Add(user.EmailAddress);
}

If you don't like those options you could pull a DirectoryEntry or read the attribute directly but other than speed I wouldn't go those routes.

如果您不喜欢这些选项,您可以直接读取DirectoryEntry或读取属性,但除了速度之外我不会去那些路线。

Also, please add appropriate error handling etc.

另外,请添加适当的错误处理等。

#1


0  

There are a few different ways to do this. Here are two simple options with few lines of code.

有几种不同的方法可以做到这一点。这是两个简单的选项,几行代码。

If your UPNs match your email addresses you could do something like this:

如果您的UPN与您的电子邮件地址匹配,您可以执行以下操作:

var userEmails = new List<string>();
foreach (var member in members)
{
    userEmails.Add(member.UserPrincipalName);
}

If that is not stringent enough and you want to actually pull the email the easiest way is probably to pull the user principal and read it's email property.

如果这不够严格并且您想要实际提取电子邮件,最简单的方法可能是拉取用户主体并读取它的电子邮件属性。

foreach (var member in members)
{
    var user = UserPrincipal.FindByIdentity(context, member.SamAccountName);
    userEmails.Add(user.EmailAddress);
}

If you don't like those options you could pull a DirectoryEntry or read the attribute directly but other than speed I wouldn't go those routes.

如果您不喜欢这些选项,您可以直接读取DirectoryEntry或读取属性,但除了速度之外我不会去那些路线。

Also, please add appropriate error handling etc.

另外,请添加适当的错误处理等。