【发布时间】:2022-07-18 02:30:19
【问题描述】:
我正在尝试使用以下代码通过 python 脚本发送邮件
import smtplib
def print_hi(name):
sender = 'my@mail.com'
receivers = ['receiver@mail.com']
message = """some msg"""
server = smtplib.SMTP('smtp.office365.com', 587)
server.set_debuglevel(1)
server.starttls()
server.ehlo()
server.login("username", "password")
server.sendmail(sender, receivers, message)
server.quit()
if __name__ == '__main__':
print_hi('PyCharm')
我收到以下错误:
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.139 Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully. Contact your administrator. [BM1PR01CA0144.INDPRD01.PROD.OUTLOOK.COM]')
我查看了许多 StackOverflow 问题和 Microsoft 博客,并获得了以下步骤来为我们的组织启用 SMTP:
- 启用 SMTP 身份验证(已启用)
- 检查安全默认值(我们无法禁用它,因为它会禁用所有 MFA,这在组织级别不符合要求)
因此,如果有人知道如何使其工作,请牢记所有安全性,这将是有帮助的!
【问题讨论】:
-
您是否为要使用的帐户创建了应用密码?我假设默认情况下,您的帐户禁用纯密码身份验证,而 SMTP 需要 oAuth 身份验证。
-
是的,甚至尝试使用应用密码但仍然没有运气。同样的错误。
标签: python email security smtp office365