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

使用imap_tools批量获取Outlook邮件时遭遇EOF error问题求助

使用imap_tools批量获取Outlook邮件时触发socket error: EOF occurred in violation of protocol

问题描述

我写了一个从Outlook收件箱获取邮件的Python脚本,默认调用fetch()时能正常运行,但传入bulk=True参数批量获取时,触发了SSL socket错误。

脚本代码

from imap_tools import MailBox

inbox = MailBox("outlook.office365.com").login("email", "password")

for email in inbox.fetch():  # bulk=True
    print(email)

错误栈

Traceback (most recent call last):
  File "/Users/julius/Library/Application Support/JetBrains/IntelliJIdea2022.3/scratches/scratch_1.py", line 5, in <module>
    for email in inbox.fetch(bulk=True):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/imap_tools/mailbox.py", line 171, in fetch
    for fetch_item in (self._fetch_in_bulk if bulk else self._fetch_by_one)(nums, message_parts, reverse):  # noqa
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/imap_tools/mailbox.py", line 144, in _fetch_in_bulk
    fetch_result = self.client.fetch(','.join(message_nums), message_parts)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 548, in fetch
    typ, dat = self._simple_command(name, message_set, message_parts)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 1230, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 1008, in _command
    raise self.abort('socket error: %s' % val)
imaplib.IMAP4.abort: socket error: EOF occurred in violation of protocol (_ssl.c:2396)

解决方案

  • 限制单次批量获取的邮件数量:Office 365的IMAP服务器对单次请求的邮件数量有上限,超过上限会触发连接中断。通过limit参数分批获取:

    from imap_tools import MailBox
    
    inbox = MailBox("outlook.office365.com").login("email", "password")
    # 每次批量获取50封,可根据实际情况调整数值
    for email in inbox.fetch(bulk=True, limit=50):
        print(email)
    
  • 调整SSL上下文配置:部分SSL协议版本兼容性问题可能导致连接中断,强制使用TLS1.2及以上版本:

    import ssl
    from imap_tools import MailBox
    
    context = ssl.create_default_context()
    # 禁用老旧的TLS版本,强制使用安全协议
    context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1
    
    inbox = MailBox("outlook.office365.com", ssl_context=context).login("email", "password")
    for email in inbox.fetch(bulk=True):
        print(email)
    
  • 使用应用专用密码:如果你的Outlook账号开启了多因素认证(MFA),普通登录密码会被拦截,需要使用应用专用密码替代普通密码登录:

    1. 登录微软账户安全中心
    2. 找到“应用密码”选项,生成一个专用密码
    3. 将脚本中的密码替换为这个应用专用密码
  • 延长连接超时时间:网络波动可能导致短时间内连接中断,设置更长的超时时间:

    from imap_tools import MailBox
    
    # 设置连接超时为30秒,默认一般是10-15秒
    inbox = MailBox("outlook.office365.com", timeout=30).login("email", "password")
    for email in inbox.fetch(bulk=True):
        print(email)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 19:50:25