通过主要Smtp地址查找Exchange用户

时间:2013-04-10 06:28:17

标签: c# outlook interop

我必须编写一个小型C#应用程序,它使用全局Outlook通讯簿从给定的电子邮件地址中查找Exchange用户。以他的名字查找Exchange用户很简单,但我如何通过他的主要smtp地址找到他?迭代整个AddressList不是一个选项,因为它是巨大的(几乎400k条目),这需要永远。有更好更快的方式吗?

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

1 个答案:

答案 0 :(得分:3)

调用Namespace.CreateRecipient,调用Recipient.Resolve,然后使用Recipient.AddressEntry属性。