Python SMTP适用于Gmail,但不适用于Office365
在我的Flask应用程序中,我尝试使用SMTP TLS发送自动电子邮件,但当我使用Gmail时,它可以正常工作,但当我尝试使用我的Office365 Outlook电子邮件时,我会收到一个身份验证错误。以下是我连接到Gmail帐户的代码(假设所需的一切都已导入):Python SMTP适用于Gmail,但不适用于Office365,python,authentication,outlook,smtp,office365,Python,Authentication,Outlook,Smtp,Office365,在我的Flask应用程序中,我尝试使用SMTP TLS发送自动电子邮件,但当我使用Gmail时,它可以正常工作,但当我尝试使用我的Office365 Outlook电子邮件时,我会收到一个身份验证错误。以下是我连接到Gmail帐户的代码(假设所需的一切都已导入): msg=MIMEMultipart() password=“” msg['From']=“” msg['To']=“” msg['Subject']=“测试” text=“测试” msg_text=MIMEText(纯文本) msg.
msg=MIMEMultipart()
password=“”
msg['From']=“”
msg['To']=“”
msg['Subject']=“测试”
text=“测试”
msg_text=MIMEText(纯文本)
msg.attach(msg_文本)
server=smtplib.SMTP('SMTP.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
#用于发送邮件的登录凭据
server.login(msg['From'],密码)
sendmail(msg['From'],msg['To'],msg.as_string())
这工作非常好,没有错误。但是,当我尝试使用带有以下代码的Office365电子邮件时(基本相同):
msg=MIMEMultipart()
password=“”
msg['From']=“”
msg['To']=“”
msg['Subject']=“测试”
text=“测试”
msg_text=MIMEText(纯文本)
msg.attach(msg_文本)
server=smtplib.SMTP('SMTP.office365.com:587')
server.ehlo()
server.starttls()
server.ehlo()
#用于发送邮件的登录凭据
server.login(msg['From'],密码)
sendmail(msg['From'],msg['To'],msg.as_string())
我收到此错误(截断为相关部分):
发送电子邮件中第68行的文件“”
server.login(msg['From'],密码)
登录名中第734行的文件“”
引发最后一个异常
登录名中第723行的文件“”
(代码,resp)=self.auth(
文件“”,第646行,在身份验证中
引发SMTPAuthenticationError(代码,resp)
smtplib.SMTPAuthenticationError:(535,b'5.7.3身份验证未成功[BY5PR16CA0014.namprd16.prod.outlook.com])
我看到了其他类似的问题,并采取了以下步骤:
- 验证用户名和密码是否正确
- 从Office365帐户中删除了两步验证
- 启用通过防火墙访问端口587
- 已验证我可以使用命令提示符中的行
连接到端口587telnet smtp.gmail.com 587
- 将
更改为smtplib.SMTP('SMTP.gmail.com:587')
smtplib.SMTP('SMTP.gmail.com','587')
- 使用SMTP_SSL而不是TLS
msg = MIMEMultipart()
password = "<Gmail password>"
msg['From'] = "<Gmail account>"
msg['To'] = "<A different Gmail account>"
msg['Subject'] = "Test"
text = "Testing"
msg_text = MIMEText(text, 'plain')
msg.attach (msg_text)
server = smtplib.SMTP('smtp.gmail.com: 587')
server.ehlo()
server.starttls()
server.ehlo()
# Login Credentials for sending the mail
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
msg = MIMEMultipart()
password = "<Office365 password>"
msg['From'] = "<Office365 Outlook account>"
msg['To'] = "<A Gmail account>"
msg['Subject'] = "Test"
text = "Testing"
msg_text = MIMEText(text, 'plain')
msg.attach (msg_text)
server = smtplib.SMTP('smtp.office365.com: 587')
server.ehlo()
server.starttls()
server.ehlo()
# Login Credentials for sending the mail
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
File "<Python file path>", line 68, in send_email
server.login(msg['From'], password)
File "<Python file path>", line 734, in login
raise last_exception
File "<Python file path>", line 723, in login
(code, resp) = self.auth(
File "<Python file path>", line 646, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful [BY5PR16CA0014.namprd16.prod.outlook.com]')