python smtp gmail身份验证错误(通过gmail smtp服务器发送电子邮件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1384535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:06:22  来源:igfitidea点击:

python smtp gmail authentication error (sending email through gmail smtp server)

pythonsmtpgmailsmtplib

提问by icn

I have the following code

我有以下代码

import smtplib
from email.mime.text import MIMEText



smtpserver = 'smtp.gmail.com'
AUTHREQUIRED = 1 # if you need to use SMTP AUTH set to 1
smtpuser = '[email protected]'  # for SMTP AUTH, set SMTP username here
smtppass = '123456'  # for SMTP AUTH, set SMTP password here

RECIPIENTS = ['[email protected]']
SENDER = '[email protected]'

msg = MIMEText('dsdsdsdsds\n')
msg['Subject'] = 'The contents of iii'
msg['From'] = '[email protected]'
msg['To'] = ''[email protected]''

mailServer = smtplib.SMTP('smtp.gmail.com',587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(smtpuser, smtppass)
mailServer.sendmail(smtpuser,RECIPIENTS,msg.as_string())
mailServer.close()

this code works fine on my desktop. but it failed with this error

这段代码在我的桌面上运行良好。但它失败了这个错误

smtplib.SMTPAuthenticationError: (535, '5.7.1 Username and Password not accepted. Learn more at\n5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 21sm4713429agd.11')

on my linux server.

在我的 linux 服务器上。

Not sure what went wrong, should i open some port on my linux server?

不知道出了什么问题,我应该在我的 linux 服务器上打开一些端口吗?

回答by Alex Martelli

Port 587 obviously needs to be open, but it probably is (or you wouldn't have gotten the detailed error msg in question). Python 2.5 vs 2.6 should make no difference. I think the issue has to do with "solving a captcha" once on the computer for which logins are currently getting rejected; follow the detailed instructions at the URL in the error message, i.e., http://mail.google.com/support/bin/answer.py?answer=14257

端口 587 显然需要打开,但它可能是(或者你不会得到有问题的详细错误信息)。Python 2.5 与 2.6 应该没有区别。我认为这个问题与在当前登录被拒绝的计算机上“解决验证码”有关;按照错误消息中 URL 的详细说明进行操作,即http://mail.google.com/support/bin/answer.py?answer=14257

回答by Raj...Deo Vindice

import random,time
for i in range(1,100):
    y=random.randint(30,300)
    time.sleep(y)
    print ("Mailing for fun, Mail No: " + str(i))
    msg = MIMEText('Testing mailing \n Mail No:' + str(i))
    msg['Subject'] = 'Mail Number: ' + str(i)

Randomizing the mail interval to check smtp behavior :)

随机化邮件间隔以检查 smtp 行为:)

With a bit addition n modification, I got this to work to check our intermittent mail bouncing.

通过一些额外的修改,我可以使用它来检查我们间歇性的邮件退回。