Outlook 加载项获取通讯组列表联系人项目的性能缓慢

slow performance of outlook addin to get contact item for distribution list

我有以下代码,它需要花费大量时间从分发列表、交换服务器中检索联系人项目。有什么办法可以调整它的性能

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())));

if debug

                 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); return null; }

    }

首先,永远不要遍历地址簿中的所有条目。您正在匹配一个地址;为什么不使用 Namespace.CreateRecipient / Recipient.Resolve

其次,您假设所有地址条目都来自您的联系人文件夹。如果是 Exchange 下的 GAL,则不是这样,AddressEntry.GetContact() 将 return null,导致异常。

如果您的条目总是来自联系人文件夹,为什么不使用 Namespace.GetDefaultFolder(olFolderContacts) 然后使用 MAPIFolder.Items.Find