通过EWS API连接到Office 365

Muf*_*lix 17 .net c# exchangewebservices office365

我在我的控制台应用程序中使用EWS API来处理邮箱项目,我的连接脚本看起来像

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.UseDefaultCredentials = true;
service.AutodiscoverUrl("emailService@domain.com");
Run Code Online (Sandbox Code Playgroud)

但我发现我的电子邮件帐户已移至Office 365云.我该如何更改身份验证?

我找到了EWS服务网址

 service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
Run Code Online (Sandbox Code Playgroud)

但我不知道如何使用它.

谢谢

Mat*_*att 18

您可以使用以下代码连接到Office 365上的EWS:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

service.Credentials = new WebCredentials("emailService@domain.com", "password");
service.AutodiscoverUrl("emailService@domain.com", RedirectionUrlValidationCallback);
Run Code Online (Sandbox Code Playgroud)

您需要为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;
}
Run Code Online (Sandbox Code Playgroud)

  • 为了能够选择正确版本的Exchange服务,例如(changeService(ExchangeVersion.Exchange2013_SP1)),您需要拥有正确版本的"Microsoft.Exchange.WebServices.dll".这对我来说引起很多头痛,希望对别人有所帮助. (2认同)