2017-05-08 42 views
0

我有以下代碼,它需要大量的時間從通訊組列表中查找聯繫人項目,交換服務器。有什麼可以做,以調整它的性能性能下降的Outlook插件獲取聯繫人項目的通訊組列表

public Outlook.AddressEntry GetDistributionListMembers(string sParamName,Outlook.Application _OutlookApp) 
    { 
     try 
     { 
      List<string> allAddressEntriesList = new List<string>(); 
      Outlook.AddressLists addrLists = _OutlookApp.ActiveExplorer().Session.AddressLists; 
      var receipientContactList = new List<Outlook.AddressEntry>(); 
      foreach (Outlook.AddressList addrList in addrLists) 
      { 
       if (addrList.AddressEntries.Count <= 0) continue; 
       receipientContactList.AddRange(addrList.AddressEntries.Cast<Outlook.AddressEntry>() 
        .Where(x => x.Address.ToLower().Equals(sParamName.ToLower()))); 

如果調試

    Debug.print(addrList.Name); 

ENDIF

   if (receipientContactList.Count > 0) break; 
      } 
      return receipientContactList.FirstOrDefault(x => x.GetContact().HasPicture) ?? 
        receipientContactList.FirstOrDefault(); 

     } 
     catch (System.Exception ex) 
     { 
      WriteLog(System.Reflection.MethodBase.GetCurrentMethod().Name, 

ex.Message); 返回null; }

} 

回答

0

首先,永遠不要通過在地址簿中的所有條目循環。你在一個地址上匹配;爲什麼不使用Namespace.CreateRecipient/Recipient.Resolve

其次,您假定所有地址條目都來自您的聯繫人文件夾。如果GAL在Exchange下,則情況並非如此,並且AddressEntry.GetContact()將返回空值,導致異常。

如果您的條目始終來自聯繫人文件夾,爲什麼不使用Namespace.GetDefaultFolder(olFolderContacts)然後使用MAPIFolder.Items.Find

+0

謝謝德米特里。我需要從本地地址條目中搜索contactitem,還需要在本地outlook實例中搜索GAL和所有可用的地址列表條目。我需要檢查聯繫人項目是否是分發列表,還是單個人,並取決於要顯示的數據。你有任何參考/鏈接看?非常感謝 –

+0

聽起來像Namespace.CreateRecipient/Recipient.Resolve是你需要的.. –

+0

謝謝我通過檢查oAddressEntry.Type ==「EX」或oAddressEntry.Type ==「SMTP」來解決它。您能否讓我知道如何查找發件人/收件人是否爲通訊組列表,並根據與其關聯的電子郵件帳戶的數量計算得出結果。謝謝 –

相關問題