如何使用EWS检索Outlook联系人?

时间:2014-05-13 11:37:50

标签: visual-studio-2010 outlook exchangewebservices

我希望我的Outlook联系人能够在网格视图中显示。 Exchange Web服务如何帮助我?

1 个答案:

答案 0 :(得分:0)

首先,从NuGet获取EWS:

Install-Package EWS-Api-2.0

接下来,建立与Exchange服务器的连接:

String username = "account@contoso.com";
String password = "mypassword";

ExchangeService ews = new ExchangeService();
ews.Credentials = new WebCredentials(username, password);

// If you have the Exchange URI, it's faster to provide it. However,
// if you don't you can auto-acquire it. User EITHER of the following:
ews.Url = new Url("https://exchange.server.com/ews/exchange.asmx"); // your own URL
ews.AutodiscoverUrl(username, x => {
  // here you can validate `x`. For testing purposes, allow all Urls
  return true;
});

接下来,我们抓住联系人:

// Contacts sit in the "contacts" folder
Folder contactsFolder = Folder.Bind(ews, WellKnownFolderName.Contacts);
// grab 100 entries at a time
ItemView itemView = new ItemView(100); // list 100 entries at a time
// retrieve first 100
FinditemsResults<item> items = contactsFolder.FindItems(itemView);

// iterate over the entries
foreach (var contact in items.OfType<Contact>())
{
    Console.WriteLine(contact.Id.UniqueId);
    Console.WriteLine("\t{0,-20}: {1}", "NickName", contact.NickName);
    Console.WriteLine("\t{0,-20}: {1}", "DisplayName", contact.DisplayName);
    Console.WriteLine("\t{0,-20}: {1}", "Email Address", contact.EmailAddresses[0]);
    Console.WriteLine();
}

从那里,您可以将上述内容置于循环中并检查items.MoreAvailable以查看是否需要再抓取另外100个条目。确保在后续调用中向offset提供ItemView参数。