我按照教程通过 python 发送电子邮件,但它返回 SMTPAuthentication 错误。下面是我的源代码:
import smtplib, getpass
# Connect to smtp server
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
print("Successfully connected to gmail.")
# User login
print("Please Login")
gmail_user = str(raw_input("Enter your email: "))
gmail_pwd = getpass.getpass("Enter your password: ")
smtpserver.login(gmail_user, gmail_pwd)
# Destination email
to = str(raw_input("Enter the email you would like to send to: \n"))
# Message contents
header = "To:" + to + "\n" + "From: " + gmail_user + "\n" + "Subject:testing \n"
print header
msg = header + '\n this is test message\n\n'
# Begin sending email
smtpserver.sendmail(gmail_user, to, msg)
print "Success!"
# Close smtpserver
smtpserver.close()
谁能告诉我这是怎么回事?我很确定我输入了正确的电子邮件和密码。谢谢!
最佳答案
我猜这是错误:
SMTPAuthenticationError: Application-specific password required
你也可以试试yagmail
的标准设置:
import yagmail
yag = yagmail.Connect(gmail_user, gmail_pwd)
yag.send(to, 'testing', 'this is test message')
yagmail 的目标是让发送 gmail 邮件变得轻松无忧。它还提供了文档中常见错误的列表。
它还建议将用户帐户保存在 key 环中,这样您就不必再写用户名/密码(尤其是不要将其写在脚本中);只需运行一次 yagmail.register(gmail_user, gmail_pwd)
希望对您有所帮助,随时查看documentation
关于python - 无法在python中登录gmail smtp服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30166105/