在 Office 365 中启用两步验证后未发送 SMTP 邮件

问题描述

我已使用该域名创建了一个 Office 365 管理员帐户。我还在我的帐户中启用了图片显示的安全信息中的两步验证:

office365 auth config

但是每当我收到一封使用此帐户的邮件时,就会出现这样的错误

2021-02-10 05:33:32 Auth method requested: UNKNowN
2021-02-10 05:33:32 Auth methods available on the server: LOGIN,XOAUTH2
2021-02-10 05:33:32 Auth method selected: LOGIN
2021-02-10 05:33:32 CLIENT -> SERVER: AUTH LOGIN
2021-02-10 05:33:32 SMTP -> get_lines(): $data is ""
2021-02-10 05:33:32 SMTP -> get_lines(): $str is  "334 VXNlcm5hbWU6
                                      "
2021-02-10 05:33:32 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2021-02-10 05:33:32 CLIENT -> SERVER: xxx
2021-02-10 05:33:32 SMTP -> get_lines(): $data is ""
2021-02-10 05:33:32 SMTP -> get_lines(): $str is  "334 UGFzc3dvcmQ6
                                      "
2021-02-10 05:33:32 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2021-02-10 05:33:32 CLIENT -> SERVER: xxx
2021-02-10 05:33:37 SMTP -> get_lines(): $data is ""
2021-02-10 05:33:37 SMTP -> get_lines(): $str is  "535 5.7.3 Authentication unsuccessful [PN0PR01CA0004.INDPRD01.PROD.outlook.com]
                                      "
2021-02-10 05:33:37 SERVER -> CLIENT: 535 5.7.3 Authentication unsuccessful [PN0PR01CA0004.INDPRD01.PROD.outlook.com]
2021-02-10 05:33:37 SMTP ERROR: Password command Failed: 535 5.7.3 Authentication unsuccessful [PN0PR01CA0004.INDPRD01.PROD.outlook.com]
2021-02-10 05:33:37 SMTP Error: Could not authenticate.
2021-02-10 05:33:37 CLIENT -> SERVER: QUIT
2021-02-10 05:33:38 SMTP -> get_lines(): $data is ""
2021-02-10 05:33:38 SMTP -> get_lines(): $str is  "221 2.0.0 Service closing transmission channel
                                      "
2021-02-10 05:33:38 SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel
2021-02-10 05:33:38 Connection: closed
2021-02-10 05:33:38 SMTP Error: Could not authenticate.
<br />
<b>Fatal error</b>:  Uncaught PHPmailerException: SMTP Error: Could not authenticate. in C:\xampp\htdocs\dede\PHPMailer\class.PHPmailer.PHP:1714
Stack trace:
#0 C:\xampp\htdocs\dede\PHPMailer\class.PHPmailer.PHP(1557): PHPMailer-&gt;smtpConnect(Array)
#1 C:\xampp\htdocs\dede\PHPMailer\class.PHPmailer.PHP(1340): PHPMailer-&gt;smtpSend('Date: Wed,10 F...','This is a multi...')
#2 C:\xampp\htdocs\dede\PHPMailer\class.PHPmailer.PHP(1215): PHPMailer-&gt;postSend()
#3 C:\xampp\htdocs\dede\register.PHP(187): PHPMailer-&gt;send()
#4 {main}

如何使用两步验证发送 SMTP 邮件?我还创建了两个活动用户,如 user1 和 user2。如果我想使用这个用户发送 SMTP 邮件,我该怎么做?

解决方法

为了使用 SMTP 身份验证,Office 365 需要启用以下设置:

Host = "smtp.office365.com"
Port = 587
SMTPAuth = true;
SMTPSecure = "tls";
SMTPAutoTLS = true;

此外,From 的值必须与 Username 的值匹配,否则 Microsoft 的 SMTP 服务器将拒绝它。

第二:检查是否为该邮箱启用了 SMTP AUTH,参见 https://docs.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission

另外:如果您必须为此帐户使用 MFA,请确保您使用应用密码:https://docs.microsoft.com/en-us/azure/active-directory/user-help/multi-factor-authentication-end-user-app-passwords