我想通过电子邮件地址从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.
有人知道快速搜索正确的AddressEntry吗?
关于, 扬
答案 0 :(得分:0)
请勿遍历容器中的所有项目-有些容器可能包含成千上万个条目或更多。
您可以在OOM中使用Namespace.CreateRecipient / Recipient.Resolve-这将为所有容器解析一个名称(或地址)。这等效于在Outlook中的“要编辑”框中键入名称,然后按Ctrl + K。
如果您要针对特定容器(例如“所有用户” GAL容器)进行解析,则需要使用扩展MAPI(仅适用于C ++或Delphi)。您可以使用Rubberduck(任何语言)-它暴露Redemption。RDOSession。ResolveName
/ ResolveNameEx
和AddressBook。ResolveName
/ ResolveNameEx
。
请记住,如果使用SMTP地址(GAL限制),则针对特定容器(MAPI中的PR_ANR分辨率)进行解析可能无效。