2017-05-08 25 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”来解决它。您能否让我知道如何查找发件人/收件人是否为通讯组列表,并根据与其关联的电子邮件帐户的数量计算得出结果。谢谢 –

相关问题