How do I get the emails of all the members of a given group in ActiveDirectory - Need help
-
Hi, I have a need to send out emails to all the people who are in a given group say "HR Department". I need to be able to get all the emails through a for loop and then as it retrieves I should send emails to them. Can some one please help me with the code how to get the emails out of AD. this is what I came up with but not sure how to go about writing a for loop to get all the emails in the given group. I am having difficulty to send both the loginName and the group that I am interested in to the AD in the search.Filter. public static string SendEmails(string loginName) { string Department = "HR Department" string userName = ExtractUserName(loginName); DirectorySearcher search = new DirectorySearcher(); search.Filter = String.Format("(SAMAccountName={0})", userName); //search.Filter = String.Format("(SAMAccountName={FAY ImprovmentForm})", userName); //LdapSearcher.Filter = string.Format("(&(objectClass=user)(department={0}))", Department); //search.Filter = string.Format("(&(objectClass=group)(SAMAccountName=" + Department + "))", userName); search.PropertiesToLoad.Add("cn"); search.PropertiesToLoad.Add("samaccountname"); search.PropertiesToLoad.Add("givenname"); search.PropertiesToLoad.Add("sn"); search.PropertiesToLoad.Add("mail"); SearchResult result = search.FindOne(); string samaccountname = (string)result.Properties["samaccountname"][0]; string givenname = (string)result.Properties["givenname"][0]; string surname = (string)result.Properties["sn"][0]; string email = (string)result.Properties["mail"][0]; if (result == null) { return "User doesn't Exist"; } else { return "Emails sent successfully!"; }
-
Hi, I have a need to send out emails to all the people who are in a given group say "HR Department". I need to be able to get all the emails through a for loop and then as it retrieves I should send emails to them. Can some one please help me with the code how to get the emails out of AD. this is what I came up with but not sure how to go about writing a for loop to get all the emails in the given group. I am having difficulty to send both the loginName and the group that I am interested in to the AD in the search.Filter. public static string SendEmails(string loginName) { string Department = "HR Department" string userName = ExtractUserName(loginName); DirectorySearcher search = new DirectorySearcher(); search.Filter = String.Format("(SAMAccountName={0})", userName); //search.Filter = String.Format("(SAMAccountName={FAY ImprovmentForm})", userName); //LdapSearcher.Filter = string.Format("(&(objectClass=user)(department={0}))", Department); //search.Filter = string.Format("(&(objectClass=group)(SAMAccountName=" + Department + "))", userName); search.PropertiesToLoad.Add("cn"); search.PropertiesToLoad.Add("samaccountname"); search.PropertiesToLoad.Add("givenname"); search.PropertiesToLoad.Add("sn"); search.PropertiesToLoad.Add("mail"); SearchResult result = search.FindOne(); string samaccountname = (string)result.Properties["samaccountname"][0]; string givenname = (string)result.Properties["givenname"][0]; string surname = (string)result.Properties["sn"][0]; string email = (string)result.Properties["mail"][0]; if (result == null) { return "User doesn't Exist"; } else { return "Emails sent successfully!"; }
This question has nothing to do with ASP.NET. Please read this article, Howto: (Almost) Everything In Active Directory via C#[^] This covered almost everything realted with AD.