有人能告诉我从Exchange Server获取联系人列表的最简单方法吗?我正在使用C#
根据我的发现, Exchange Web服务仅适用于Exchange Server 2007及更高版本。这将是我的第一个选择,但我也想要替代以前版本的Exchange(WebDav或其他东西)。目录服务不是一种选择。
答案 0 :(得分:12)
这是如何使用EWS从您的联系人列表中获取联系人列表。我不确定如何从全局列表中获取联系人,仅在一小时前查看了API。
private static void ListContacts(ExchangeService svc) {
foreach (var v in svc.FindItems(WellKnownFolderName.Contacts,
new ItemView(20))) {
Contact contact = v as Contact;
ContactGroup contactGroup = v as ContactGroup;
//v.Load(); // Turns out you don't need to load for basic props.
if (contact != null) {
Console.WriteLine("Contact: {0} <{1}>",
contact.DisplayName,
contact.EmailAddresses[EmailAddressKey.EmailAddress1]);
} else if (contactGroup != null) {
Console.WriteLine("Contact Group: {0}", contactGroup.DisplayName);
switch (svc.RequestedServerVersion) {
case ExchangeVersion.Exchange2007_SP1:
ExpandGroupResults groupResults
= svc.ExpandGroup((contactGroup.Id));
foreach (var member in groupResults) {
Console.WriteLine("+ {0} <{1}>",
member.Name, member.Address);
}
break;
case ExchangeVersion.Exchange2010:
foreach (GroupMember member in contactGroup.Members) {
Console.WriteLine("+ {0} <{1}>",
member.AddressInformation.Name,
member.AddressInformation.Address);
}
break;
default:
Console.WriteLine(
"** Unknown Server Version: {0}",
svc.RequestedServerVersion);
break;
}
} else {
Console.WriteLine("Unknown contact type: {0} - {1}",
contact.GetType(), v.Subject);
}
}
}
我已经无视创建动词服务,请查看Exchange Web Services API以获取更多信息。
答案 1 :(得分:1)
首先,不要忘记添加对Microsoft Exchange Web服务库的引用。
private static void ConnectToExchangeService()
{
service = new ExchangeService();
service.Credentials = new WebCredentials(USERNAME, PASSWORD, DOMAIN_NAME);
service.AutodiscoverUrl(USER_ADDRESS);
}
private static void ListGlobalContacts(ExchangeService service)
{
/* passing true as the third parameter to "ResolveName" is important to
make sure you get the contact details as well as the mailbox details */
NameResolutionCollection searchResult = service.ResolveName(NAME_YOURE_LOOKING_FOR, ResolveNameSearchLocation.DirectoryOnly, true);
foreach (NameResolution resolution in searchResult )
{
Console.WriteLine("name is " + resolution.Contact.DisplayName);
Console.WriteLine("address is " + resolution.Mailbox.Address);
Console.WriteLine("business phone is " + resolution.Contact.PhoneNumbers[PhoneNumberKey.BusinessPhone]);
Console.WriteLine("mobile phone is " + resolution.Contact.PhoneNumbers[PhoneNumberKey.MobilePhone]);
}
}
...而且Brett Ryan已经提供了获取本地联系人列表的代码。
这种检索全局联系人列表的方法(好吧,至少其中一个)的问题是,“ResolveName”函数最多可以返回100个联系人,这样如果你的组织有更多的记录,你就在麻烦。一种可能的解决方法(以及我实现的解决方法)是对每个字母进行单独搜索(假设您可以验证此类搜索将始终返回少于100个结果)并将所有唯一条目串在一起列为一个列表。