如何获取交换公用文件夹的所有电子邮件地址列表?
将自行回复,将接受所提供的最佳回复。
答案 0 :(得分:3)
虽然您发布的自己的答案可行,但阅读您正在使用的方法和对象的文档有助于了解其局限性。如果您多次调用此代码,最终会导致内存泄漏。 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.
}
}
}
}
}
DirectorySearcher.SizeLimit的备注表示如果大小限制高于服务器确定的默认值(1000个条目),则会忽略大小限制。分页允许您根据需要获得所需的所有条目。
DirectorySearcher.FindAll()的评论提到需要处理SearchResultCollection以释放资源。将其包含在using
语句中可以清楚地表明您作为程序员的意图。
如果您使用的是Exchange 2007或2010,则还可以安装Exchange管理工具并使用powershell cmdlet查询公用文件夹。您可以实际创建powershell运行空间并直接调用Exchange cmdlet,而无需实际需要用户与之交互的控制台。
答案 1 :(得分:-1)
以下代码将获取交换公用文件夹的所有电子邮件地址列表。
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);
}
}