2014-09-01 108 views
0

我需要從交換/活動目錄中獲取所有電子郵件的列表。
無論是電子郵件如[email protected]還是電子郵件羣組,如全部聯繫人或CEO,他們都包含幾個電子郵件地址。
這是我到目前爲止的代碼:
C#列出MS Exchange中的所有電子郵件地址

DirectoryEntry de = new DirectoryEntry(ad_path); 
DirectorySearcher ds = new DirectorySearcher(de); 
ds.Filter = "(&(objectClass=addressBookContainer)(CN=All Global Address Lists,CN=Address Lists Container,CN=First Organization,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=mydomain,DC=local))"; 
SearchResultCollection ss = ds.FindAll(); // count = 0 

回答

1

你不會得到Mailadresses這些目錄的直接對象,這些僅僅是配置對象。如果你只是想獲得的所有Mailadresses在你的組織,你可以簡單地查詢以下(注意,默認情況下,有限的resultsize):

 DirectoryEntry de = new DirectoryEntry(); 
     DirectorySearcher ds = new DirectorySearcher(de); 
     ds.PropertiesToLoad.Add("proxyAddresses"); 
     ds.Filter = "(&(proxyAddresses=smtp:*))"; 
     SearchResultCollection ss = ds.FindAll(); // count = 0 

     foreach (SearchResult sr in ss) 
     {// you might still need to filter out other addresstypes, ex: sip: 
      foreach (String addr in sr.Properties["proxyAddresses"]) 
       Console.WriteLine(addr); 
//or whithout the 'smtp:' prefix Console.WriteLine(addr.SubString(5)); 

     } 

如果你想獲得具體的交換地址列表中的內容,你可以修改你的過濾器,並與該列表的「purportedSearch'-屬性的值替換它,例如:

(&(mailNickname=*)(|(objectClass=user)(objectClass=contact)(objectClass=msExchSystemMailbox)(objectClass=msExchDynamicDistributionList)(objectClass=group)(objectClass=publicFolder))) 

這是‘默認全局地址列表’默認過濾器。或者,您可以枚舉(CN =所有全局地址列表,CN =地址列表容器,CN =第一個組織,CN = Microsoft Exchange,CN =服務,CN =配置,DC = mydomain,DC =本地)中的所有AddressBookContainer對象用每個「purportedSearch」屬性進行查詢。

相關問題