【问题标题】:Find Exchange User via Primary Smtp Address通过主 Smtp 地址查找 Exchange 用户
【发布时间】:2013-04-11 02:25:06
【问题描述】:

我必须编写一个小型 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;
    }

【问题讨论】:

    标签: c# outlook interop


    【解决方案1】:

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

    【讨论】:

    • 非常感谢,一直在寻找这个。像魅力一样工作。
    • 嗨,您的方式适用于较新版本的 Outlook(我正在使用 addin express)。但同样的方法在 v.2003 中会出现错误。它标记 Outlook.ExchangeUser 和 addressEntry.GetExchangeUser();红色的。请建议我解决这个问题。谢谢
    • 在 Outlook 2003 中,没有 ExchangeUser obejkct 并且没有 AddressEntry.PropertyAccessor 来读取原始 MAPI 属性。是否可以选择使用兑换?
    猜你喜欢
    • 1970-01-01
    • 2018-11-17
    • 2016-09-16
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多