简体   繁体   English

通过 EWS API 连接到 Office 365

[英]Connection to Office 365 by EWS API

I am using EWS API in my console application to process mailbox items and my connection script looks like我在控制台应用程序中使用 EWS API 来处理邮箱项目,我的连接脚本看起来像

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.UseDefaultCredentials = true;
service.AutodiscoverUrl("emailService@domain.com");

But i found that my email account was moved to Office 365 cloud.但我发现我的电子邮件帐户已移至 Office 365 云。 How should i change the authentication ?我应该如何更改身份验证?

i found EWS service url我找到了 EWS 服务网址

 service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

but i dont know how to use it.但我不知道如何使用它。

Thank you谢谢

You can use the code below to connect to the EWS on office 365:您可以使用以下代码连接到 Office 365 上的 EWS:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

service.Credentials = new WebCredentials("emailService@domain.com", "password");
service.AutodiscoverUrl("emailService@domain.com", RedirectionUrlValidationCallback);

You need define one callback function for the AutodiscoveryUrl function, like this:您需要为 AutodiscoveryUrl 函数定义一个回调函数,如下所示:

private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
    // The default for the validation callback is to reject the URL.
    bool result = false;

    Uri redirectionUri = new Uri(redirectionUrl);

    // Validate the contents of the redirection URL. In this simple validation
    // callback, the redirection URL is considered valid if it is using HTTPS
    // to encrypt the authentication credentials. 
    if (redirectionUri.Scheme == "https")
    {
        result = true;
    }
    return result;
}

I know this is a fairly old solution, but it was still very helpful to me.我知道这是一个相当古老的解决方案,但它对我仍然非常有帮助。 I have a few tools that worked with the "normal" network version of Exchange, but so far my tests with Exchange Online failed (i got errors like "The Autodiscover service couldn't be located", etc).我有一些工具可以与 Exchange 的“普通”网络版本一起使用,但到目前为止,我对 Exchange Online 的测试失败了(我遇到了“无法找到自动发现服务”等错误)。

Essential here is to use WebCredentials instead of NetworkCredential and a e-mailaddress instead of a username.此处必不可少的是使用 WebCredentials 而不是 NetworkCredential 和电子邮件地址而不是用户名。

You cannot use basic authentication (username and password) in your EWS application to connect to Office/Microsoft 365 now.现在无法在 EWS 应用程序中使用基本身份验证(用户名和密码)连接到 Office/Microsoft 365。 Microsoft no longer supports basic authentication in EWS for Exchange Online. Microsoft 不再支持 EWS for Exchange Online 中的基本身份验证。 You are advised to use OAuth2.0 to get a token and use the same in your EWS client to connect to Office 365.建议您使用 OAuth2.0 获取令牌并在 EWS 客户端中使用相同的令牌连接到 Office 365。

For this you will have to register your application in Azure AD to use client credential flow.为此,您必须在 Azure AD 中注册您的应用程序才能使用客户端凭据流。 I did a POC on this using console application.我使用控制台应用程序对此进行了 POC。 在此处输入图片说明

在此处输入图片说明

Here's the link to GitHub repository for source code and documentation.这是指向GitHub存储库的源代码和文档的链接。

There appears to have been a few changes in EWS connection to office365 in regards to security, causing Matt's answer to not work for me.在安全方面,EWS 与 office365 的连接似乎发生了一些变化,导致 Matt 的回答对我不起作用。

What did work is the following:做了什么工作如下:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1)
{
     Credentials = new WebCredentials("user", "password", "domain"),
     Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx")
};

Importat notes:进口注意事项:

  • AutodiscoverUrl did not complete successfully, it failed to discover the url every time AutodiscoverUrl没有成功完成,每次都发现url失败

  • user must be the full email address of the user. user必须是user完整电子邮件地址

  • domain is the NetBIOS domain name , meaning it is only the domain name. domainNetBIOS 域名,意思是它只是域名。 You can find the domain in the Microsoft 365 admin center under Settings -> Domains .您可以在 Microsoft 365 管理中心的Settings -> Domains下找到该域。 The easiest way is to find the fallback domain in the form of domain.onmicrosoft.com .最简单的方法是以domain.onmicrosoft.com的形式查找回退域。 Take only the domain part.只取domain部分。

  • The user must be a Microsoft 365 admin用户必须是 Microsoft 365 管理员

  • Any change in one of these parameters caused an Unauthorized exeption.这些参数之一的任何更改都会导致Unauthorized例外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM