metacircle
metacircle

Reputation: 2588

Find Exchange User via Primary Smtp Address

I have to write a small C# App which is using the Global Outlook Address Book to find the Exchange User from a given E-Mail Address. Finding the Exchange User by his name is simple, but how do I find him by his primary smtp address? Iterating over the whole AddressList is not an option, since it is huge (almost 400k Entries) this takes forever. Is there a better and faster way?

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

Upvotes: 0

Views: 2036

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66341

Call Namespace.CreateRecipient, call Recipient.Resolve, then use the Recipient.AddressEntry property.

Upvotes: 3

Related Questions