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

使用authorization_code授权类型配置SMTP OAuth时遇535 5.7.3认证失败

使用Authorization Code授权类型获取Token后SMTP认证失败(535 5.7.3)

我在Azure中注册了应用,通过Postman获取Access Token。当使用Client Credentials作为授权类型获取Token时,测试用的Python脚本可正常发送测试邮件;但使用Authorization Code作为授权类型获取Token时,会出现535 5.7.3 Authentication unsuccessful错误。

已确认以下内容:

  • 目标邮箱允许SMTP连接
  • 无CA策略拦截流量
  • 应用已获得SMTP.Send权限且管理员已完成同意操作
  • 能正常获取到Authorization Code类型的Access Token

目前已提交微软工单,等待升级支持(通用支持团队因我使用脚本测试而拒绝提供支持)。

测试脚本代码:

import smtplib
import base64
import os
import ssl

smtp_server = "smtp.office365.com" #smtp-mail.outlook.com  #smtp.office365.com
smtp_port = 587
from_addr = "xxxxxxxx" #os.getenv("SMTP_USER")
to_addr = "xxxxxx"
access_token = "xxxxxxxx" 

#os.getenv("SMTP_ACCESS_TOKEN")

if not access_token:
    print("Error: Access token not set in SMTP_ACCESS_TOKEN")
    exit(1)

# Compose XOAUTH2 string
auth_string = f"user={from_addr}\x01auth=Bearer {access_token}\x01\x01"
auth_b64 = base64.b64encode(auth_string.encode('utf-8')).decode('utf-8')

try:
    smtp = smtplib.SMTP(smtp_server, smtp_port)
    smtp.set_debuglevel(1)  # Enable verbose logging
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    code, resp = smtp.docmd("AUTH", "XOAUTH2 " + auth_b64)
    print("AUTH response:", code, resp.decode('utf-8'))

    if code != 235:
        print("Failed XOAUTH2 Auth!")
        smtp.quit()
        exit(1)

    msg = f"From: {from_addr}\r\nTo: {to_addr}\r\nSubject: Test mail via SMTP OAuth2!\r\n\r\nThis is a test."
    smtp.sendmail(from_addr, [to_addr], msg)
    print("Mail sent!")
except smtplib.SMTPException as e:
    print(f"SMTP error: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")
finally:
    smtp.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 04:42:08