通过主Smtp地址查找Exchange用户 [英] Find Exchange User via Primary Smtp Address
问题描述
我必须编写一个小型C#App,该App正在使用Global Outlook通讯簿从给定的电子邮件地址中查找Exchange用户.通过他的名字找到Exchange用户很简单,但是如何通过他的主要smtp地址找到他呢?遍历整个AddressList并不是一个选择,因为它庞大(几乎400k条目),这将永远花费.有没有更好,更快的方法?
I have to write a small C# App which is using the Global Outlook Address Book to find the Exchange User from a given E-Mail Address. Finding the Exchange User by his name is simple, but how do I find him by his primary smtp address? Iterating over the whole AddressList is not an option, since it is huge (almost 400k Entries) this takes forever. Is there a better and faster way?
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;
}
推荐答案
调用Namespace.CreateRecipient,调用Recipient.Resolve,然后使用Recipient.AddressEntry属性.
Call Namespace.CreateRecipient, call Recipient.Resolve, then use the Recipient.AddressEntry property.
这篇关于通过主Smtp地址查找Exchange用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!