升级到 O365 破坏了 EWS 自动发现

Upgrade to O365 broke EWS Autodiscover

我公司刚刚将一些邮箱迁移到O365。不幸的是,这会破坏使用 EWS 创建的应用程序。尝试调用 AutodiscoverUrl() 时遇到错误。

'The Autodiscover service couldn't be located.'

代码:

        service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        service.UseDefaultCredentials = true;
        service.AutodiscoverUrl(mailbox, RedirectionCallback);

        private bool RedirectionCallback(string url)
        {
            return true; 
        }

我也试过将 URL 设置为以下

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

这些都没有解决问题。有谁知道从这里去哪里?

  • service.UseDefaultCredentials 应该是假的,因为你需要电子邮件+密码(作为安全字符串)才能连接
  • 使用最新的 ExchangeVersion 值
  • url 是 https://outlook.office365.com/EWS/Exchange.asmx

    public ExchangeService Connect()
    {
        var lastExchangeVersion = Enum.GetValues(typeof(ExchangeVersion)).Cast<ExchangeVersion>().ToList().Last();
        var service = new ExchangeService(lastExchangeVersion)
        {
            Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"),
            Credentials = new NetworkCredential(_cloudEmail, _cloudPassword)
        };
        return service;
    }
    public SecureString ConvertStringToSecure(string password)
    {
        if (string.IsNullOrWhiteSpace(password)) return null;
        var result = new SecureString();
        foreach (char c in password) result.AppendChar(c);
        return result;
    }