14

谁能告诉我从 Exchange Server 获取联系人列表的最简单方法?我正在使用 C#

据我所知,Exchange Web Services仅适用于 Exchange Server 2007 及更高版本。那将是我的第一个选择,但我也想要以前版本的 Exchange(WebDav 或其他东西)的替代方案。目录服务不是一个选项。

4

2 回答 2

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 服务 API以获取更多信息。

于 2010-02-09T08:53:58.150 回答
1

首先,不要忘记添加对 Microsoft Exchange Webservices Library 的引用。

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 个结果)并将所有唯一条目串在一起到一个列表中。

于 2016-06-02T06:37:13.437 回答