1

我想从 Outlook 中获取特定分发列表下所有用户的用户名和电子邮件 ID。

我有下面的代码。

                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”方法无法获取任何内容。

我的代码有什么问题吗??

有人给我示例代码,它可以获取分发列表下所有用户的用户名和电子邮件 ID?

任何帮助表示赞赏。

先感谢您!!

4

2 回答 2

0

试试这个链接。

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

它对我有用。它也应该对你有用。代码如下:

        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) + "<Br>";
                int selIndex = objName.IndexOf("CN=") + 3;
                int selEnd = objName.IndexOf(",OU") - 3;
                str1 += objName.Substring(selIndex, selEnd).Replace("\\", "") + "<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());
于 2014-11-28T06:31:08.657 回答
0
        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) + "&lt;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;


    }
}

这将为您提供用逗号分隔的电子邮件

于 2018-05-20T09:45:08.740 回答