【问题标题】:Fast searching an Outlook Address list快速搜索 Outlook 地址列表
【发布时间】:2019-02-02 02:27:21
【问题描述】:

我想通过电子邮件地址从 Outlook 中搜索一个巨大的地址列表以查找 AddressEntry。按名称搜索不是问题,您可以这样写:

Microsoft.Office.Interop.Outlook.AddressEntry = AddressEntries[Name];

但我想通过它的电子邮件找到条目。此代码正在运行,但速度极慢:

    public static string GetUserDataByEmailAddress(string EmailAddress)
    {
        Microsoft.Office.Interop.Outlook.Application OLApp = null;
        bool OutlookWasRunning = false;
        string UserName = string.Empty;

        if (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Count() > 0)
        {
            OLApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
            OutlookWasRunning = true;
        }
        else
        {
            OLApp = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.NameSpace nameSpace = OLApp.GetNamespace("MAPI");
            nameSpace.Logon("", "", System.Reflection.Missing.Value, System.Reflection.Missing.Value);
            nameSpace = null;
            OutlookWasRunning = false;
        }

        Microsoft.Office.Interop.Outlook.AddressLists GALs = null;
        GALs = OLApp.Session.AddressLists;
        if (GALs == null) { throw new System.Exception("ERROR: Unable to get address book collection from MS Outlook!"); }

        Microsoft.Office.Interop.Outlook.AddressList gal = null;
        gal = GALs["Globale Adressliste"];
        if (gal == null) { throw new System.Exception("ERROR: Unable to get address book 'Global Address List' from MS Outlook!"); }

        foreach (Microsoft.Office.Interop.Outlook.AddressEntry ent in gal.AddressEntries)
        {
            if(ent.Address == EmailAddress) { UserName = ent.Name; }
        }

        if (!OutlookWasRunning) { OLApp.Quit(); }

        return UserName;
    }

好的,但是这种方式运行速度很慢。现在,我尝试使用 AddressEntries 作为 IEnumeratable 来执行此操作:

var output = from a in gal.AddressEntries.AsQueryable() where (a as Microsoft.Office.Interop.Outlook.AddressEntry).Address == EmailAddress select a;

这样做,我得到了错误:

Severity Code Description Project File Line Suppression State Error CS1936 Could not find an implementation of the query pattern for source type 'IQueryable'. 'Where' not found.

有人知道快速搜索正确地址条目的方法吗?

问候, 一月

【问题讨论】:

    标签: c# outlook interop addressbook


    【解决方案1】:

    不要循环遍历容器中的所有项目 - 某些容器可能包含数万个或更多条目。

    您可以在 OOM 中使用 Namespace.CreateRecipient / Recipient.Resolve - 这将针对所有容器解析名称(或地址)。这相当于在 Outlook 的“收件人”编辑框中键入名称并按 Ctrl+K。

    如果您想针对特定容器(例如“所有用户”GAL 容器)进行解析,则需要使用扩展 MAPI(仅限 C++ 或 Delphi)。您可以使用Redemption(任何语言) - 它公开RDOSession.AddressBook.ResolveName / ResolveNameExRDOAddresList.ResolveName / ResolveNameEx

    请记住,如果使用 SMTP 地址(GAL 限制),则针对特定容器进行解析(MAPI 中的 PR_ANR 解析)可能不起作用。

    【讨论】:

    • 谢谢。是的,这个容器包含超过 400.000 人,并且与交换服务器的连接似乎很慢。即使我使用AddressList.AddressEntries.Cast<Microsoft.Office.Interop.Outlook.AddressEntry>().ToList() 也需要很长时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    相关资源
    最近更新 更多