【发布时间】: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