In AD here at work, we have some security groups that are mail enabled. I am using the System.DirectoryServices.AccountManagement namespace like so:
List<GroupPrincipal> result = new List<GroupPrincipal>();
using (PrincipalContext domain = new PrincipalContext(ContextType.Domain, userinfo[0]))
using (UserPrincipal user = UserPrincipal.FindByIdentity(domain, username))
{
if (user != null)
{
PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
int totalGroupCounter = 0;
StringBuilder output = new StringBuilder();
List<GroupPrincipal> securityGroups = new List<GroupPrincipal>();
List<GroupPrincipal> distributionGroups = new List<GroupPrincipal>();
foreach (Principal group in groups)
{
totalGroupCounter++;
if (((GroupPrincipal)group).IsSecurityGroup.Value)
securityGroups.Add((GroupPrincipal)group);
else
distributionGroups.Add((GroupPrincipal)group);
}
}
}
Armed with this info, what's the correct way to find the group's email address?
The AccountManagement libraries limit which properties you can access. If you want to get the email property for a group, you'll need to cast it back to a DirectoryEntry
object.
PropertyValueCollection email = ((DirectoryEntry)group.GetUnderlyingObject()).Properties["mail"];
if (email.Value != null)
{
// Do something with email property
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With