Pan
Pan

Reputation: 25

Reading username and emailid of members under outlook distribution list using C#

I want to fetch username and email id of all users under particular Distribution List from outlook.

I have the below code.

                DirectorySearcher search;
                DirectoryEntry entry;

                entry = new DirectoryEntry("LDAP://" + Domain);
                search = new DirectorySearcher(entry);
                search.Filter = "(&(objectClass=Distribution Lists) (CN=" + distList + "))";

                //search.Filter = "CN=" + distList;
                //search.Filter = "cn=" + distList + ",ou=Distribution Lists,dc=bosch,dc=com";
                //search.Filter = "cn=" + distList + ",ou=Distribution Lists";
                int i = search.Filter.Length;


                string str = "", str1 = "";
                foreach (SearchResult AdObj in search.FindAll())
                {


                    foreach (String objName in                  AdObj.GetDirectoryEntry().Properties["member"])
                    {
                        COnsole.writeline(objName);
}
}

'FindAll' method is not able to fetch anything.

Is there anything wrong with my code??

CAn some one give me sample code which can fetch username and email Id of all users under Distribution List??

Any help is appreciated.

Thank you in advance!!

Upvotes: 1

Views: 2620

Answers (2)

Rushabh Shah
Rushabh Shah

Reputation: 1

        entry = new DirectoryEntry("LDAP Path");//, Domainwithuser, password);
        search = new DirectorySearcher(entry);
        search.Filter = "CN=#nameofyourDL";
        int i = search.Filter.Length;


        string str = "", str1 = "",final="";
        foreach (SearchResult AdObj in search.FindAll())
        {


            foreach (String objName in AdObj.GetDirectoryEntry().Properties["member"])
            {
                str += Convert.ToString(objName) + "<Br>";
                int selIndex = objName.IndexOf("CN=") + 3;
                int selEnd = objName.IndexOf(",OU") - 3;
                str1 += objName.Substring(selIndex, selEnd).Replace("\\", "");


                DirectorySearcher dsSearch = new DirectorySearcher(entry);
                dsSearch.Filter = "CN=" + objName.Substring(selIndex, selEnd).Replace("\\", "");
                foreach (SearchResult rs in dsSearch.FindAll())
                {
                    //str1 += "&lt;p align='right'><font face='calibri' color='#2266aa' size=2>" + Convert.ToString(rs.GetDirectoryEntry().Properties["mail"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["displayName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["sAMAccountName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["department"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["memberOf"].Value) + "&lt;/font></p>";
                    str1 = Convert.ToString(rs.GetDirectoryEntry().Properties["mail"].Value);
                    final += str1 + ",";
                }
                //final=str1+",";
            }

        }
        //("&lt;BR>" + str + "&lt;Br>" + str1 + "&lt;BR>");


        return final;
    }
    catch (Exception ex)
    {
        //Response.Write("--unable to fetch--<BR>" + ex.Message);
        throw ex;


    }
}

This will give you emails separated by commas

Upvotes: 0

Gowrisankar R
Gowrisankar R

Reputation: 1

Try this link.

http://forums.asp.net/t/1224607.aspx?Displaying+Members+in+a+Distribution+List

It worked for me. It should work for you too. The code is as follows:

        DirectorySearcher search;
        DirectoryEntry entry;          

        entry = new DirectoryEntry(LDAPpath);//, Domainwithuser, password);
        search = new DirectorySearcher(entry);
        search.Filter = "CN=DistributionList1";
        int i = search.Filter.Length;

        string str = "", str1 = "";
        foreach (SearchResult AdObj in search.FindAll())
        {
            foreach (String objName in AdObj.GetDirectoryEntry().Properties["member"])
            {
                str += Convert.ToString(objName) + "&lt;Br>";
                int selIndex = objName.IndexOf("CN=") + 3;
                int selEnd = objName.IndexOf(",OU") - 3;
                str1 += objName.Substring(selIndex, selEnd).Replace("\\", "") + "&lt;BR>";

                DirectorySearcher dsSearch = new DirectorySearcher(entry);
                dsSearch.Filter = "CN=" + objName.Substring(selIndex, selEnd).Replace("\\", "");
                foreach (SearchResult rs in dsSearch.FindAll())
                {
                    str1 += "&lt;p align='right'><font face='calibri' color='#2266aa' size=2>" + Convert.ToString(rs.GetDirectoryEntry().Properties["mail"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["displayName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["sAMAccountName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["department"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["memberOf"].Value) + "&lt;/font></p>";
                }
            }
        }
        Response.Write("&lt;BR>" + str + "&lt;Br>" + str1 + "&lt;BR>");

        MessageBox.Show(rs.GetDirectoryEntry().Properties["sAMAccountName"].Value.ToString() + " : " + rs.GetDirectoryEntry().Properties["mail"].Value.ToString());

Upvotes: 0

Related Questions