You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于IIS的应用调用Office 365 EWS发邮件遇权限与XML错误求助

Office 365 EWS 401未授权及XML格式错误排查求助

我们的应用部署在Windows Server 2019 IIS上,需要通过指定Office 365邮箱账号密码登录,自动向客户端发送邮件,核心是调用EWS接口完成登录并发送邮件。

问题及排查过程

  • 最初使用EWS官方URL https://outlook.office365.com/EWS/Exchange.asmx,返回**(401) Unauthorized**错误。
    已完成排查:确认邮箱关闭双因素认证、账号密码无误、委托权限配置正确;也尝试了自动发现URL https://autodiscover.outlook.com/EWS/Exchange.asmx,问题未解决。
  • 更换URL为https://outlook.office.com/mail/ourdomain.co.za/EWS/Exchange.asmx后,能进入Office 365登录页,但返回The response received from the service didn't contain valid XML错误。

环境信息

  • 未部署Exchange混合解决方案,仅使用纯Office 365服务,无本地Exchange服务器
  • 已排查防火墙规则,未发现请求被拦截
  • 尝试在IIS请求筛选中添加并允许目标URL,无效果

当前代码

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

//Set your Exchange server URL and credentials.
try
{
    // Set the correct URL for Office 365.
    service.Url = new Uri("https://outlook.office.com/mail/ourdomain.co.za/EWS/Exchange.asmx");
}
catch (Exception ex)
{
    return BadRequest("WebService Uri: " + ex.Message);
}


try
{
    service.Credentials = new WebCredentials("****user@ourdomain.co.za****", "****userpassword****");
}
catch (Exception ex)
{
    return BadRequest("Credentials: " + ex.Message);
}

try
{
    EmailMessage email = new EmailMessage(service);
    email.Subject = "Your Visits for today";
    email.Body = new MessageBody("The email is working");
    email.ToRecipients.Add("****Recipients@ourdomain.co.za****");
    email.Send();

    return Ok("Email sent");
}
catch (Exception ex)
{
    return BadRequest("Message Error: " + ex.Message);
}
}

内容的提问来源于stack exchange,提问作者Eddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 00:33:11