使用C#读取Outlook分发列表下的成员的用户名和电子邮件ID

时间:2013-02-25 05:59:06

标签: c#-3.0 outlook-2003

我想从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吗?

感谢任何帮助。

提前谢谢!!

2 个答案:

答案 0 :(得分: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());

答案 1 :(得分: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;


    }
}

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