Find Exchange User via Primary Smtp Address

时光总嘲笑我的痴心妄想 提交于 2019-12-12 01:09:24

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/15918536/find-exchange-user-via-primary-smtp-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!