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

使用exchangelib读取Webmail邮件时遇MalformedResponseError(404)问题

解决exchangelib连接Webmail时的MalformedResponseError(404状态码)

问题根源

404状态码表明请求找不到Exchange Web Services (EWS)的端点,结合响应头里的Apache和PHP/8.1.18信息,大概率是以下两种情况之一:

  • 目标Webmail系统(比如Roundcube、Horde这类开源Webmail)根本不支持EWS协议,exchangelib仅适配Exchange Server或兼容EWS的邮件服务;
  • 配置的EWS服务路径不正确,默认的EWS端点路径可能被自定义修改了。

解决方案

方案1:确认目标邮箱是否支持EWS

如果你的Webmail不是Exchange Server或微软365这类兼容EWS的服务,放弃使用exchangelib,改用IMAP/SMTP协议读取邮件。示例代码(基于imaplib):

import imaplib

def connect_mail_imap(username, password, imap_server, port=993):
    # 连接IMAP服务器(默认SSL端口993)
    mail = imaplib.IMAP4_SSL(imap_server, port)
    mail.login(username, password)
    # 选择收件箱
    mail.select('INBOX')
    return mail

# 使用示例
username = "xxx@xxx.com"
password = "xxxxxxxxxx"
imap_server = "mail.xxxx.com"
mail_conn = connect_mail_imap(username, password, imap_server)
# 后续可调用mail_conn.search()等方法读取邮件

方案2:若确认支持EWS,修正EWS端点配置

默认EWS的端点路径是https://<server>/EWS/Exchange.asmx,但部分部署会修改这个路径。需确认正确的EWS URL,然后在Configuration中显式指定service_endpoint参数:

from exchangelib import Account,Credentials,Configuration,DELEGATE 
from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter
BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter

def connect_mail(username, password, address):
    credentials = Credentials(username=username, password=password)
    # 显式指定EWS服务端点
    config = Configuration(
        server='mail.xxxx.com', 
        credentials=credentials,
        auth_type='NTLM',
        service_endpoint='https://mail.xxxx.com/EWS/Exchange.asmx'  # 替换为实际的EWS路径
    )
    account = Account(
        address,
        credentials=credentials,
        autodiscover=False,
        config=config, 
        access_type=DELEGATE
    )
    return account

username = "xxx@xxx.com"
password = "xxxxxxxxxx"
address = "xxx@xxx.com"
account = connect_mail(username, password, address)

额外排查点

  • 确认服务器协议:如果邮件服务用的是HTTP而非HTTPS,需在Configuration中设置protocol='http';
  • 验证认证方式:NTLM不一定是正确的认证类型,部分服务可能使用Basic,可尝试修改auth_type='Basic'测试;
  • SSL证书检查:当前代码中NoVerifyHTTPAdapter已跳过证书验证,测试环境可用,生产环境建议导入信任证书。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 23:27:21