交换公共文件夹的电子邮件地址列表

发布于 2024-08-09 14:54:07 字数 64 浏览 16 评论 0原文

如何获取 Exchange 公用文件夹的所有电子邮件地址的列表?

我会自己回复,会接受最好的回复。

How do i get a list of all e-mail address for exchange public folders?

Will reply on my own, will accept the best reply offered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里°也失望 2024-08-16 14:54:07

虽然您作为自己的答案发布的内容可行,但阅读您正在使用的方法和对象的文档以了解它们的局限性会有所帮助。如果您多次调用此代码,您最终会遇到内存泄漏。 foreach 语句不会对所使用的对象调用 Dispose(),仅调用它创建的枚举器。下面是一种更好的搜索目录的方法(尽管很少进行错误检查并且没有异常处理)。

public static void GetPublicFolderList()
{
    DirectoryEntry entry = new DirectoryEntry("LDAP://sorcogruppen.no");
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = "(&(objectClass=publicfolder))";
    // Request the mail attribute only to reduce the ammount of traffic
    // between a DC and the application.
    mySearcher.PropertiesToLoad.Add("mail");

    // See Note 1
    //mySearcher.SizeLimit = int.MaxValue;

    // No point in requesting all of them at once, it'll page through
    // all of them for you.
    mySearcher.PageSize = 100;

    // Wrap in a using so the object gets disposed properly.
    // (See Note 2)
    using (SearchResultCollection searchResults = mySearcher.FindAll())
    {
        foreach (SearchResult resEnt in searchResults)
        {
            // Make sure the mail attribute is provided and that there
            // is actually data provided.
            if (resEnt.Properties["mail"] != null
                 && resEnt.Properties["mail"].Count > 0)
            {
                string email = resEnt.Properties["mail"][0] as string;
                if (!String.IsNullOrEmpty(email))
                {
                    // Do something with the email address
                    // for the public folder.
                }
            }
        }
    }
}

注 1

DirectorySearcher.SizeLimit 的备注指示如果大小限制高于服务器确定的默认值(1000 个条目),则忽略该大小限制。分页使您可以根据需要获取所需的所有条目。

注2

DirectorySearcher.FindAll() 提到需要处置 SearchResultCollection 以释放资源。将其包装在 using 语句中可以清楚地表明您作为程序员的意图。

额外

如果您使用的是 Exchange 2007 或 2010,您还可以安装 Exchange 管理工具并使用 powershell cmdlet 查询公用文件夹。您可以务实地创建 powershell 运行空间并直接调用 Exchange cmdlet,而实际上不需要用户与之交互的控制台。

While what you posted as your own answer would work, it helps to read the documentation for the methods and objects you are using to understand their limitations. If you had called this code multiple times you would eventually had a memory leak on your hands. The foreach statement doesn't call Dispose() on the object used, only the enumerator it creates. Below is a somewhat better method of searching the directory (though very little error checking and no exception handling).

public static void GetPublicFolderList()
{
    DirectoryEntry entry = new DirectoryEntry("LDAP://sorcogruppen.no");
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = "(&(objectClass=publicfolder))";
    // Request the mail attribute only to reduce the ammount of traffic
    // between a DC and the application.
    mySearcher.PropertiesToLoad.Add("mail");

    // See Note 1
    //mySearcher.SizeLimit = int.MaxValue;

    // No point in requesting all of them at once, it'll page through
    // all of them for you.
    mySearcher.PageSize = 100;

    // Wrap in a using so the object gets disposed properly.
    // (See Note 2)
    using (SearchResultCollection searchResults = mySearcher.FindAll())
    {
        foreach (SearchResult resEnt in searchResults)
        {
            // Make sure the mail attribute is provided and that there
            // is actually data provided.
            if (resEnt.Properties["mail"] != null
                 && resEnt.Properties["mail"].Count > 0)
            {
                string email = resEnt.Properties["mail"][0] as string;
                if (!String.IsNullOrEmpty(email))
                {
                    // Do something with the email address
                    // for the public folder.
                }
            }
        }
    }
}

Note 1

The remarks for DirectorySearcher.SizeLimit indicate that the size limit is ignored if it is higher than the server-determined default (1000 entries). Paging allows you to get all of the entries you need as you need them.

Note 2

The remarks for DirectorySearcher.FindAll() mention that the SearchResultCollection needs to be disposed to release resources. Wrapping it in a using statement clearly identifies your intent as a programmer.

Extra

If you're using Exchange 2007 or 2010 you could also install the Exchange Management Tools and use the powershell cmdlets to query your public folders. You can pragmatically create a powershell runspace and call the Exchange cmdlets directly without actually needing a console for the user to interact with.

花开雨落又逢春i 2024-08-16 14:54:07

以下代码将获取 Exchange 中公用文件夹的所有电子邮件地址的列表。

public static void GetPublicFolderList()
{
 DirectoryEntry entry = new DirectoryEntry("LDAP://FakeDomain.com");
 DirectorySearcher mySearcher = new DirectorySearcher(entry);
 mySearcher.Filter = "(&(objectClass=publicfolder))";
 mySearcher.SizeLimit = int.MaxValue;
 mySearcher.PageSize = int.MaxValue;            

 foreach (SearchResult resEnt in mySearcher.FindAll())
 {
  if (resEnt.Properties.Count == 1)
   continue;

  object OO = resEnt.Properties["mail"][0];
 }
}

如果您想要公用文件夹的所有电子邮件地址,

请删除:

object OO = resEnt.Properties["mail"][0];

添加:
for (int counter = 0; counter < resEnt.Properties["proxyAddresses"].Count; counter++)

{
 string Email = (string)resEnt.Properties["proxyAddresses"][counter];
 if (Email.ToUpper().StartsWith("SMTP:"))
 {
  Email = Email.Remove(0, "SMTP:".Length);
 }
}

The following code will get a list of all email address of public folders in exchange.

public static void GetPublicFolderList()
{
 DirectoryEntry entry = new DirectoryEntry("LDAP://FakeDomain.com");
 DirectorySearcher mySearcher = new DirectorySearcher(entry);
 mySearcher.Filter = "(&(objectClass=publicfolder))";
 mySearcher.SizeLimit = int.MaxValue;
 mySearcher.PageSize = int.MaxValue;            

 foreach (SearchResult resEnt in mySearcher.FindAll())
 {
  if (resEnt.Properties.Count == 1)
   continue;

  object OO = resEnt.Properties["mail"][0];
 }
}

If you want All email addresses of the public folder,

remove:

object OO = resEnt.Properties["mail"][0];

Add:
for (int counter = 0; counter < resEnt.Properties["proxyAddresses"].Count; counter++)

{
 string Email = (string)resEnt.Properties["proxyAddresses"][counter];
 if (Email.ToUpper().StartsWith("SMTP:"))
 {
  Email = Email.Remove(0, "SMTP:".Length);
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文