2013-04-10 21 views
0

我必須編寫一個小型C#應用程序,它使用全局Outlook通訊簿從給定的電子郵件地址查找Exchange用戶。通過他的名字找到Exchange用戶很簡單,但我如何通過他的主SMTP地址找到他?遍歷整個AddressList不是一個選項,因爲它是巨大的(將近400k條目),這需要永遠。有更好更快的方法嗎?通過主Smtp地址查找交換用戶

public Outlook.ExchangeUser GetAddressBookEntry(string senderName, string senderAddress) 
    { 
     //Get Outlook address book 
     Outlook.AddressList addressList = olNamespace.AddressLists["Globale Adressliste"]; 
     Outlook.AddressEntries addressEntries = addressList.AddressEntries; 

     Outlook.ExchangeUser exUser = null; 


     //Find corresponding entry in the address book 
     //This always returns something even if the SenderName is not in the Address Book 
     if (senderName != null) 
     { 
      Outlook.AddressEntry addressEntry = addressEntries[senderName]; 
      exUser = addressEntry.GetExchangeUser(); 
     } 

     //Check if contact is correct (see above for reason) 
     if (exUser != null && ((exUser.Name == senderName) || (exUser.PrimarySmtpAddress == senderAddress))) 
     { 
      return exUser; 
     } 

     //this loop takes a few minutes, it is not an option 
     //not checking the address not implemented 
     Debug.WriteLine("Count: " + addressEntries.Count); 

     Stopwatch sw = new Stopwatch(); 
     sw.Start(); 
     for (int i = 1; i <= addressEntries.Count; i++) 
     { 
      Outlook.AddressEntry addressEntry = addressEntries[i]; 

      if (i % 1000 == 0) 
      { 
       Debug.WriteLine(i); 
      } 
     } 
     sw.Stop(); 
     Debug.WriteLine("Seconds: " + sw.Elapsed.TotalSeconds); 

     return null; 
    } 

回答

3

調用Namespace.CreateRecipient,調用Recipient.Resolve,然後使用Recipient.AddressEntry屬性。

+0

非常感謝,一直在尋找這一段時間。奇蹟般有效。 – metacircle 2013-04-12 06:09:41

+0

嗨,你的方式是爲較新版本的Outlook工作(我正在使用addin express)。但是同樣的方法在v.2003中給出了錯誤。它標記爲Outlook.ExchangeUser和addressEntry.GetExchangeUser();紅色的。請建議我解決這個問題。謝謝 – 2015-03-19 14:11:53

+0

在Outlook 2003中沒有ExchangeUser obejkct,並且沒有AddressEntry.PropertyAccessor可讀取原始MAPI屬性。正在使用Redemption選項嗎? – 2015-03-19 14:24:02