Python SMTP Gmail 认证错误(通过 Gmail SMTP 发送邮件)

3 投票
2 回答
14156 浏览
提问于 2025-04-15 14:06

我有以下这段代码:

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

这段代码在我的电脑上运行得很好,但在我的Linux服务器上却出现了这个错误:

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服务器上打开某个端口呢?

2 个回答

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的表现 :)

通过一些小的修改和添加,我让这个功能正常工作,以检查我们偶尔出现的邮件退回问题。

6

端口587显然需要打开,但它可能已经打开了(否则你不会收到这么详细的错误信息)。Python 2.5和2.6之间应该没有区别。我觉得问题可能和“在当前被拒绝登录的电脑上解决一次验证码”有关;请按照错误信息中的网址提供的详细说明进行操作,也就是http://mail.google.com/support/bin/answer.py?answer=14257

撰写回答