Gmail SMTP拒绝我的登录信息

时间:2014-05-17 00:09:32

标签: python smtp gmail

smtplib.SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvuQ\n5.7.14 hjav3RshZ9XqmApuN6mVTPJ_3AZUJEkiniSxdgdVMrgEpKpUtHi8_oCjzuOA9pkhGMyTrs\n5.7.14 fuSX9EuvWudU00Q1KXZgY4rZ1I5ZEEDOqvVMl7bOQitwyb_sYdgPA3tJC7_xpUN1zDC6Ib\n5.7.14 MjA2mM_oMdCOeCpodh-13LwLFlyzmZALwg2uu522OxG0NH74B2hafBfT2F1XK0lXCz1hce\n5.7.14 3yugD0g> Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 bw2sm40059670pad.46 - gsmtp')

我从以下脚本中收到上述错误。是的,我已经确认我使用的是正确的凭据。我想做的就是从脚本发送电子邮件!有没有人遇到过这个问题?

import smtplib

FROMADDR = "my.real.address@gmail.com"
LOGIN    = FROMADDR
PASSWORD = "my.real.password"
TOADDRS  = ["my.real.address@gmail.com"]
SUBJECT  = "Test"

msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
       % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += "some text\r\n"

server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()

3 个答案:

答案 0 :(得分:2)

您必须在Google帐户安全部分中启用“访问安全性较低的应用”选项。

答案 1 :(得分:1)

尝试将代码更改为:

server.ehlo()
server.starttls()
server.ehlo()  # you are missing elho to establish communication with server
server.login(username, password)

# Full script
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

from_address = 'you@gmail.com'
to_address = 'you80@gmail.com'
text = 'test message sent from Python'
username = '****'
password = '****'
msg = MIMEMultipart()
msg['From'] = from_address
msg['To'] = to_address
msg['Subject'] = 'Foo'
msg.attach(MIMEText(text))
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username, password)
server.sendmail(from_address, to_address, msg.as_string())
server.quit()

如果您因为错误消息而未使用浏览器登录,那么您也应该尝试使用,如果您有许多不成功的登录,则必须输入验证码,

答案 2 :(得分:0)

此解决方案最终改变了我的Gmail密码。我从来没有弄清楚哪些特殊字符会丢掉所有内容,但我只是生成了一个新密码,之后没有任何问题。