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

时间:2009-09-05 23:52:32

标签: python smtp gmail smtplib

我有以下代码

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 = 'admin@myhost.com'  # for SMTP AUTH, set SMTP username here
smtppass = '123456'  # for SMTP AUTH, set SMTP password here

RECIPIENTS = ['online8@gmail.com']
SENDER = 'admin@myhost.com'

msg = MIMEText('dsdsdsdsds\n')
msg['Subject'] = 'The contents of iii'
msg['From'] = 'admin@myhost.com'
msg['To'] = ''online8@gmail.com''

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()

此代码在我的桌面上运行正常。但它失败了这个错误

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')

在我的linux服务器上。

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

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:2)

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)

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

稍加一点修改,我就可以检查我们间歇性的邮件弹跳了。