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