python - 无法在python中登录gmail smtp服务器

标签 python error-handling smtp gmail server

我按照教程通过 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/

相关文章:

java - javamail 中使用的 mail.smtp.sendpartial 属性是否特定于 sun 实现?

parsing - header 解析+MIME

azure - 通过 smtp 从 azure 中安装为 IaaS 的服务器发送电子邮件

python - 创建一个变量/DataFrame,从函数的参数中获取其名称

python - 在 Python 中批量转换 Unix 时间戳的函数

python - 从子目录导入模块的案例 - 为什么它有效?

python - 如何将 defaultdict(list) 转换为 Pandas DataFrame

c# - 发生异常时显示窗体 C#

python - 如何区分 ValueError 的情况

php - 调用 php 中未定义的方法