Exchange パブリック フォルダーのすべての電子メール アドレスの一覧を取得するにはどうすればよいですか?
自分で返信します。提供された最良の返信を受け入れます。
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.
}
}
}
}
}
DirectorySearcher.SizeLimitの注釈は、サイズ制限がサーバーによって決定された既定値 (1000 エントリ) よりも大きい場合、サイズ制限が無視されることを示しています。ページングを使用すると、必要なときに必要なすべてのエントリを取得できます。
DirectorySearcher.FindAll()の注釈には、リソースを解放するために SearchResultCollection を破棄する必要があることが記載されています。それをusingステートメントにまとめることで、プログラマーとしての意図を明確に識別できます。
Exchange 2007 または 2010 を使用している場合は、Exchange 管理ツールをインストールし、powershell コマンドレットを使用してパブリック フォルダーにクエリを実行することもできます。PowerShell 実行空間を実用的に作成し、ユーザーが対話するためのコンソールを実際に必要とせずに、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 カウンター = 0; カウンター < resEnt.Properties["proxyAddresses"].Count; カウンター++)
{
string Email = (string)resEnt.Properties["proxyAddresses"][counter];
if (Email.ToUpper().StartsWith("SMTP:"))
{
Email = Email.Remove(0, "SMTP:".Length);
}
}