交換するパブリックフォルダーへのメールアドレスのリスト
-
12-09-2019 - |
質問
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.
}
}
}
}
}
注1
に対するコメント DirectorySearcher.SizeLimit サイズ制限がサーバーによって決定されたデフォルト (1000 エントリ) より大きい場合、サイズ制限が無視されることを示します。ページングを使用すると、必要なすべてのエントリを必要なときに取得できます。
注2
に対するコメント DirectorySearcher.FindAll() リソースを解放するには SearchResultCollection を破棄する必要があることに注意してください。で包む using
このステートメントは、プログラマーとしての意図を明確に示しています。
余分な
Exchange 2007 または 2010 を使用している場合は、Exchange 管理ツールをインストールし、PowerShell コマンドレットを使用してパブリック フォルダーをクエリすることもできます。実際にユーザーが操作するためのコンソールを必要とせずに、PowerShell 実行空間を作成し、Exchange コマンドレットを直接呼び出すことができます。
他のヒント
次のコードは、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];
追加します。
用(INTカウンタ= 0;カウンタ{
string Email = (string)resEnt.Properties["proxyAddresses"][counter];
if (Email.ToUpper().StartsWith("SMTP:"))
{
Email = Email.Remove(0, "SMTP:".Length);
}
}