C# 如何从 Exchange Server 获取联系人列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2071668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:15:21  来源:igfitidea点击:

How to get contact list from Exchange Server?

c#exchange-serverexchangewebservices

提问by Johnny

Can anyone tell me the simplest way to get a contact list from Exchange Server? I'm using C#

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

From what I found out, Exchange Web Servicesonly exists for Exchange Server 2007 and beyond. That would be my first option, but I'd also like an alternative for previous versions of Exchange (WebDav or something). Directory Services is not an option.

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

采纳答案by Brett Ryan

This is how to get the contact list from your contacts list in exchange using EWS. I'm not sure how to get contacts from the global list yet, only looked at the API an hour ago.

这是如何使用 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);
        }
    }
}

I've ommited creating the service for verbocity, have a look at the Exchange Web Services APIfor more information.

我已经省略了为冗长创建服务,请查看Exchange Web 服务 API以获取更多信息。

回答by shai tibber

First of all, don't forget to add a reference to the Microsoft Exchange Webservices Library.

首先,不要忘记添加对 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]);
    }
}

...and Brett Ryan already supplied the code for getting the local contacts list.

...并且 Brett Ryan 已经提供了获取本地联系人列表的代码。

The problem with this method of retrieving the global contacts list (well, one of them at least) is that the function "ResolveName" returns up to 100 contacts so that if your organization has more records than that, you're in trouble. One possible workaround (and the one I implemented) is to conduct a seperate search for each letter (assuming you can verify that such a search will always return less than 100 results) and string all the unique entries together into one list.

这种检索全局联系人列表的方法(好吧,至少是其中之一)的问题在于函数“ResolveName”最多返回 100 个联系人,因此如果您的组织有更多的记录,您就有麻烦了。一种可能的解决方法(也是我实施的方法)是对每个字母进行单独搜索(假设您可以验证此类搜索将始终返回少于 100 个结果)并将所有唯一条目一起串成一个列表。