如何获取 Exchange 公用文件夹的所有电子邮件地址的列表?
我会自己回复,会接受最好的回复。
虽然您作为自己的答案发布的内容可行,但阅读您正在使用的方法和对象的文档以了解它们的局限性会有所帮助。如果您多次调用此代码,您最终会遇到内存泄漏。这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.
}
}
}
}
}
Note 1
备注为目录搜索器.SizeLimit http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.sizelimit.aspx指示如果大小限制高于服务器确定的默认值(1000 个条目),则忽略该大小限制。分页使您可以根据需要获取所需的所有条目。
Note 2
备注为DirectorySearcher.FindAll() http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.findall.aspx提到需要处置 SearchResultCollection 以释放资源。将其包裹在一个using
声明清楚地表明了您作为程序员的意图。
Extra
如果您使用的是 Exchange 2007 或 2010,您还可以安装 Exchange 管理工具并使用 powershell cmdlet 查询公用文件夹。您可以务实地创建 powershell 运行空间并直接调用 Exchange cmdlet,而实际上不需要用户与之交互的控制台。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)