0

I have the following code it takes lot of time to retrieve contact items from the distribution list, exchange server. Is there anything can be done to tune it s performance

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; }

    }
Jyothi Srinivasa
  • 701
  • 2
  • 9
  • 26

1 Answers1

0

Firstly, never loop through all entries in the address book. You are matching on an address; why not use Namespace.CreateRecipient / Recipient.Resolve?

Secondly, you are assuming that all address entries come from your Contacts folder. In case of GAL under Exchange, this is not the case, and AddressEntry.GetContact() will return null, resulting in an exception.

And if your entries always come from the Contacts folder, why not use Namespace.GetDefaultFolder(olFolderContacts) and then use MAPIFolder.Items.Find?

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Thanks Dmitry. I need to search contactitem from local address entry and also GAL and all available address list entries in local outlook instance. I need to be checking if contact item is distributionlist, or individual and depending on that data to be displayed. Do you have any references/links to look at ? Many thanks – Jyothi Srinivasa May 08 '17 at 20:21
  • Thanks I have resolved it by checking oAddressEntry.Type == "EX" or oAddressEntry.Type == "SMTP". Can you please let me know how to find if the sender/recpient is a distribution list and get count if the number of email accounts associated with it. thanks – Jyothi Srinivasa May 09 '17 at 07:56
  • 1
    Check is AddressEntry.Members is not null and then check Members.Count – Dmitry Streblechenko May 09 '17 at 14:21