How do I get the emails of all the members of a given group in ActiveDirectory - Need help RRS feed

  • Question

  • User661550187 posted


      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

    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.

    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);
            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";
                return "Emails sent successfully!";


    Thursday, March 11, 2010 5:11 PM

All replies

  • User-845805365 posted

    static string ADAdminUser = "YourUserName";
            static string ADAdminPassword = "YourPassword";
            static string ADFullPath = "LDAP://YourDomainName.com";
            public static DirectoryEntry GetDirectoryEntry(string DomainReference)
                DirectoryEntry de = new DirectoryEntry(ADFullPath + DomainReference, ADAdminUser, ADAdminPassword, AuthenticationTypes.Secure);
                return de;
            private static string GetLDAPDomain()
                StringBuilder LDAPDomain = new StringBuilder();
                string[] LDAPDC = "YourDomainName.com".Split('.');
                for (int i = 0; i < LDAPDC.GetUpperBound(0) + 1; i++)
                    LDAPDomain.Append("DC=" + LDAPDC[i]);
                    if (i < LDAPDC.GetUpperBound(0))
                return LDAPDomain.ToString();
            private void btSendMail_Click(object sender, EventArgs e)
            public void GetUsersForGroupAndSendMail(string GroupName)
                DataSet dsUser = new DataSet();
                DirectoryEntry de = GetDirectoryEntry("/" + GetLDAPDomain());
                //create instance fo the direcory searcher
                DirectorySearcher deSearch = new DirectorySearcher();
                //set the search filter
                deSearch.SearchRoot = de;
                deSearch.Filter = "(&(objectClass=group)(cn=" + GroupName + "))";
                //get the group result
                SearchResult results = deSearch.FindOne();
                //if the group is valid, then continue, otherwise return a blank dataset
                if (results != null)
                    //create a link to the group object, so we can get the list of members
                    //within the group
                    DirectoryEntry deGroup = new DirectoryEntry(results.Path, ADAdminUser, ADAdminPassword, AuthenticationTypes.Secure);
                    //assign a property collection
                    System.DirectoryServices.PropertyCollection pcoll = deGroup.Properties;
                    int n = pcoll["member"].Count;
                    //if there are members fo the group, then get the details and assign to the table
                    for (int l = 0; l < n; l++)
                        //create a link to the user object sot hat the FirstName, LastName and SUername can be gotten
                        DirectoryEntry deUser = new DirectoryEntry(ADFullPath + "/" + pcoll["member"][l].ToString(), ADAdminUser, ADAdminPassword, AuthenticationTypes.Secure);
                        //rwUser["UserName"] = deUser.Properties["cn"][0].ToString();
                        //rwUser["DisplayName"] = deUser.Properties["givenName"][0].ToString() + " " + deUser.Properties["sn"][0].ToString();
                        string email = string.Empty;
                        if (deUser.Properties.Contains("mail"))
                            email = deUser.Properties["mail"][0].ToString();
                        //close the directory entry object
            private void SendEmailToUser(string email)
                //Write your code to send the mail

    Tuesday, March 30, 2010 3:05 AM