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

启用双因素认证的Outlook账户如何用Python发送邮件?

解决启用双因素认证的Outlook账户Python SMTP发送邮件问题

启用双因素认证(MFA)的Outlook账户无法用普通密码通过SMTP登录,核心解决方案是使用微软应用密码替代普通密码,以下是完整实现步骤:

1. 生成应用密码

应用密码是微软为启用MFA的账户提供的专用密码,用于第三方应用登录:

  • 登录微软账户安全中心
  • 进入「高级安全选项」
  • 找到「应用密码」选项,点击「创建」
  • 系统会生成一个16位的密码,务必立即保存,此密码仅显示一次

2. Python发送邮件代码实现

使用smtplib和email库,配置Outlook官方SMTP服务器:

import smtplib
from email.mime.text import MIMEText

# 替换为你的账户信息
sender = "your-account@outlook.com"
app_pwd = "生成的16位应用密码"
receiver = "target-account@outlook.com"

# 构造邮件内容
msg = MIMEText("这是启用MFA的Outlook账户发送的测试邮件", "plain", "utf-8")
msg["From"] = sender
msg["To"] = receiver
msg["Subject"] = "MFA账户SMTP测试邮件"

# 发送邮件
try:
    with smtplib.SMTP("smtp.office365.com", 587) as smtp_server:
        smtp_server.starttls()  # 启用TLS加密,必须步骤
        smtp_server.login(sender, app_pwd)
        smtp_server.send_message(msg)
    print("邮件发送成功")
except Exception as e:
    print(f"发送失败: {str(e)}")

注意事项

  • 应用密码仅适用于不支持MFA验证的第三方应用,无法用于网页版Outlook登录
  • 若为Office 365商业账户,需确认管理员已开启「允许应用密码」权限,否则无法生成应用密码
  • Outlook SMTP服务器固定为smtp.office365.com,端口587,必须使用starttls()启用加密,不推荐直接使用SSL端口465

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 20:45:57