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

无法通过Azure注册登录IMAP4 Outlook邮箱,代码卡在starttls步骤

问题:Azure OAuth2登录Outlook IMAP4邮箱时starttls()卡住报错

以下是用于通过Azure OAuth2登录IMAP4 Outlook邮箱的Python代码:

import imaplib
import msal

imap_filter = '(FROM "xxxxxxxxxxxx@maybank.com" SINCE "SINCE 13-Jul-2023" BEFORE "BEFORE 17-Jul-2023")'

email_user = 'xxxxxxxxxx@aarna.capital'
email_host = 'smtp.office365.com'
port = 993

mail = imaplib.IMAP4_SSL(email_host,port)

conf = {
            "authority": "https://login.microsoftonline.com/xxxxxxxxxxxxxxx",
            "client_id": "xxxxxxxxxxxxxxxxxx", #AppID
            "scope": ['https://outlook.office365.com/.default'],
            "secret": "xxxxxxxxxxxxxxxxxxxxxxxxx", #Key-Value
            "secret-id": "xxxxxxxxxxxxxxxxxxxxxx", #Key-ID
        }

def generate_auth_string(user, token):
    return f"user={user}\x01auth=Bearer {token}\x01\x01"    

app = msal.ConfidentialClientApplication(conf['client_id'], authority=conf['authority'],
                                         client_credential=conf['secret'])

result = app.acquire_token_silent(conf['scope'], account=None)

if not result:
    print("No suitable token in cache.  Get new one.")
    result = app.acquire_token_for_client(scopes=conf['scope'])
    print(result)
if "access_token" in result:
    print(result['token_type'])
    print(result)
else:
    print(result.get("error"))
    print(result.get("error_description"))
    print(result.get("correlation_id"))

mail = imaplib.IMAP4('smtp.office365.com')
mail.debug = 4
mail.starttls()
mail.authenticate("XOAUTH2", lambda x: generate_auth_string(email_user, result['access_token']).encode("utf-8"))

问题详情

  • 代码已正常运行一年,近期突然失效
  • 确认凭证均正常,且能成功生成access_token
  • 执行至mail.starttls()步骤时卡住,报错信息为12:08.14 > b'EMFF1 STARTTLS'
  • 当前使用TLS 1.2版本,参考相关方案后仍未解决问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 01:12:17