C# 使用Microsoft Exchange Web服务从Outlook检索联系人

C# 使用Microsoft Exchange Web服务从Outlook检索联系人,c#,contacts,exchangewebservices,C#,Contacts,Exchangewebservices,我正在使用Microsoft Exchange Services从Outlook检索联系人。我使用以下代码。它执行时没有任何错误。然而,我在联系人中什么也得不到 public ActionResult Index() { ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); _service.Credentials = new WebCredent

我正在使用Microsoft Exchange Services从Outlook检索联系人。我使用以下代码。它执行时没有任何错误。然而,我在联系人中什么也得不到

public ActionResult Index()
    {
        ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        _service.Credentials = new WebCredentials("username", "password");
        _service.AutodiscoverUrl("****");
        _service.Url = new Uri("https://***/EWS/Exchange.asmx");
        foreach (Contact contact in _service.FindItems(WellKnownFolderName.Contacts, new ItemView(int.MaxValue)))
        {
            // do something 
        }
        return View();
    }
我怎样才能得到联系人

请帮忙,
谢谢。

我建议你清理你的代码,因为有一些原因我可以看到它失败了

    _service.AutodiscoverUrl("****");
    _service.Url = new Uri("https://***/EWS/Exchange.asmx");
使用一个或另一个,对于AutoDiscoveryURL,您可能需要在此处进行回调

    foreach (Contact contact in _service.FindItems(WellKnownFolderName.Contacts, new ItemView(int.MaxValue)))
首先,Contacts文件夹可以包含Contacts以外的对象,因此,如果代码遇到通讯组列表,它将生成异常。另外,使用int.MaxValue是一个坏主意,您应该以1000人为一组对项目进行分页(Exchange 2010上的限制将强制您执行此操作,因此如果联系人超过1000人,您的代码将无法获取所有联系人)。您尝试访问的邮箱是否也属于您使用的安全凭据。我建议你用类似的东西

        String mailboxToAccess = "user@domain.onmicrosoft.com";
        ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        _service.Credentials = new WebCredentials("upn@domain.onmicrosoft.com", "password");
        _service.AutodiscoverUrl(mailboxToAccess, redirect => true);
       // _service.Url = new Uri("https://***/EWS/Exchange.asmx");
        ItemView iv = new ItemView(1000);
        FolderId ContactsFolderId = new FolderId(WellKnownFolderName.Contacts,mailboxToAccess);
        FindItemsResults<Item> fiResults;
        do
        {
            fiResults = _service.FindItems(ContactsFolderId, iv);
            foreach (Item itItem in fiResults.Items)
            {
                if (itItem is Contact)
                {
                    Contact ContactItem = (Contact)itItem;
                    Console.WriteLine(ContactItem.Subject);
                }
            }
            iv.Offset += fiResults.Items.Count;
        } while (fiResults.MoreAvailable);
String mailboxToAccess=”user@domain.onmicrosoft.com";
ExchangeService\u服务=新的ExchangeService(ExchangeVersion.Exchange2010\u SP1);
_service.Credentials=新的WebCredentials(“upn@domain.onmicrosoft.com“,”密码“);
_AutodiscoverUrl(mailboxToAccess,redirect=>true);
//_service.Url=新Uri(“https://***/EWS/Exchange.asmx”);
ItemView iv=新的ItemView(1000);
FolderId ContactsFolderId=新的FolderId(WellKnownFolderName.Contacts,mailboxToAccess);
FindItemsResults;
做
{
fiResults=_service.FindItems(ContactsFolderId,iv);
foreach(fiResults.Items中的项目itItem)
{
如果(itItem为Contact)
{
联系人联系人项目=(联系人)itItem;
Console.WriteLine(ContactItem.Subject);
}
}
iv.偏移量+=fiResults.Items.Count;
}而(fiResults.MoreAvailable);
您可以使用EWS编辑器测试EWS本身