我试图使用python发送电子邮件(Gmail),但我得到以下错误。
Traceback (most recent call last):
File "emailSend.py", line 14, in <module>
server.login(username,password)
File "/usr/lib/python2.5/smtplib.py", line 554, in login
raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.
Python脚本如下所示。
import smtplib
fromaddr = 'user_me@gmail.com'
toaddrs = 'user_you@gmail.com'
msg = 'Why,Oh why!'
username = 'user_me@gmail.com'
password = 'pwd'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
这是
创建Gmail APP密码!
创建之后,创建一个名为sendgmail.py的文件
然后添加以下代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# Created By : Jeromie Kirchoff
# Created Date: Mon Aug 02 17:46:00 PDT 2018
# =============================================================================
# Imports
# =============================================================================
import smtplib
# =============================================================================
# SET EMAIL LOGIN REQUIREMENTS
# =============================================================================
gmail_user = 'THEFROM@gmail.com'
gmail_app_password = 'YOUR-GOOGLE-APPLICATION-PASSWORD!!!!'
# =============================================================================
# SET THE INFO ABOUT THE SAID EMAIL
# =============================================================================
sent_from = gmail_user
sent_to = ['THE-TO@gmail.com', 'THE-TO@gmail.com']
sent_subject = "Hey Friends!"
sent_body = ("Hey, what's up? friend!\n\n"
"I hope you have been well!\n"
"\n"
"Cheers,\n"
"Jay\n")
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(sent_to), sent_subject, sent_body)
# =============================================================================
# SEND EMAIL OR DIE TRYING!!!
# Details: http://www.samlogic.net/articles/smtp-commands-reference.htm
# =============================================================================
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_app_password)
server.sendmail(sent_from, sent_to, email_text)
server.close()
print('Email sent!')
except Exception as exception:
print("Error: %s!\n\n" % exception)
所以,如果你成功了,你会看到这样的图像:
我通过给自己和自己发送电子邮件进行测试。
注意:我在我的帐户上启用了两步验证。应用程序密码与此工作!(对于gmail SMTP设置,您必须访问https://support.google.com/accounts/answer/185833?hl=en并遵循以下步骤)
此设置对于启用了两步验证的帐户不可用。这样的帐户需要特定于应用程序的密码来访问不太安全的应用程序。
澄清
导航到https://myaccount.google.com/apppasswords,并创建一个APP密码如上所述。
来自@David的很棒的回答,这里是Python 3没有通用的try-除了:
def send_email(user, password, recipient, subject, body):
gmail_user = user
gmail_pwd = password
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
这是
创建Gmail APP密码!
创建之后,创建一个名为sendgmail.py的文件
然后添加以下代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# Created By : Jeromie Kirchoff
# Created Date: Mon Aug 02 17:46:00 PDT 2018
# =============================================================================
# Imports
# =============================================================================
import smtplib
# =============================================================================
# SET EMAIL LOGIN REQUIREMENTS
# =============================================================================
gmail_user = 'THEFROM@gmail.com'
gmail_app_password = 'YOUR-GOOGLE-APPLICATION-PASSWORD!!!!'
# =============================================================================
# SET THE INFO ABOUT THE SAID EMAIL
# =============================================================================
sent_from = gmail_user
sent_to = ['THE-TO@gmail.com', 'THE-TO@gmail.com']
sent_subject = "Hey Friends!"
sent_body = ("Hey, what's up? friend!\n\n"
"I hope you have been well!\n"
"\n"
"Cheers,\n"
"Jay\n")
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(sent_to), sent_subject, sent_body)
# =============================================================================
# SEND EMAIL OR DIE TRYING!!!
# Details: http://www.samlogic.net/articles/smtp-commands-reference.htm
# =============================================================================
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_app_password)
server.sendmail(sent_from, sent_to, email_text)
server.close()
print('Email sent!')
except Exception as exception:
print("Error: %s!\n\n" % exception)
所以,如果你成功了,你会看到这样的图像:
我通过给自己和自己发送电子邮件进行测试。
注意:我在我的帐户上启用了两步验证。应用程序密码与此工作!(对于gmail SMTP设置,您必须访问https://support.google.com/accounts/answer/185833?hl=en并遵循以下步骤)
此设置对于启用了两步验证的帐户不可用。这样的帐户需要特定于应用程序的密码来访问不太安全的应用程序。
澄清
导航到https://myaccount.google.com/apppasswords,并创建一个APP密码如上所述。
我遇到过类似的问题,并无意中发现了这个问题。我得到一个SMTP身份验证错误,但我的用户名/ pass是正确的。下面是解决问题的方法。我读到:
https://support.google.com/accounts/answer/6010255
简而言之,谷歌不允许您通过smtplib登录,因为它已经标记了这种登录为“不太安全”,所以你要做的是去这个链接,而你登录到你的谷歌帐户,并允许访问:
https://www.google.com/settings/security/lesssecureapps
一旦设置好了(见下面的截图),它就可以工作了。
登录现在工作:
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login('me@gmail.com', 'me_pass')
更改后的回复:
(235, '2.7.0 Accepted')
响应之前:
smtplib.SMTPAuthenticationError: (535, '5.7.8 Username and Password not accepted. Learn more at\n5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 g66sm2224117qgf.37 - gsmtp')
还是不行?如果您仍然得到SMTPAuthenticationError,但现在代码是534,这是因为位置未知。点击这个链接:
https://accounts.google.com/DisplayUnlockCaptcha
点击继续,这应该给你10分钟的时间来注册你的新应用。所以现在继续进行另一次登录尝试,它应该可以工作。
更新:这似乎不能马上工作,你可能会卡在smptlib中得到这个错误:
235 == 'Authentication successful'
503 == 'Error: already authenticated'
该消息说使用浏览器登录:
SMTPAuthenticationError: (534, '5.7.9 Please log in with your web browser and then try again. Learn more at\n5.7.9 https://support.google.com/mail/bin/answer.py?answer=78754 qo11sm4014232igb.17 - gsmtp')
启用“lesssecureapps”后,去喝杯咖啡,回来后再次尝试“DisplayUnlockCaptcha”链接。从用户体验来看,更改可能需要一个小时才能生效。然后再次尝试登录过程。
你需要在直接运行到STARTTLS之前说EHLO:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
此外,您应该真正创建From:、To:和Subject:消息头,用空行与消息体分隔,并使用CRLF作为EOL标记。
E.g.
msg = "\r\n".join([
"From: user_me@gmail.com",
"To: user_you@gmail.com",
"Subject: Just a message",
"",
"Why, oh why"
])
注意:
为了做到这一点,你需要在gmail帐户配置中启用“允许不太安全的应用程序”选项。否则,当gmail检测到一个非谷歌应用程序试图登录你的帐户时,你会得到一个“关键安全警报”。