无法使用python

时间:2016-05-27 12:38:25

标签: python email smtp

我正在尝试使用python发送电子邮件。我在Stackoverflow上尝试了很多解决方案,但都没有。

我的代码是 -

import smtplib
fromaddr = 'user_me@gmail.com'
toaddrs  = 'user_you@gmail.com'
msg = 'msg text here'
username = 'user_me@gmail.com'
password = 'pwd'
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

代码仍然停留在第server = smtplib.SMTP('smtp.gmail.com',587)

尝试了端口465并收到此错误 -

Traceback (most recent call last):
  File "gmail-python.py", line 130, in <module>
    mainFunction()
  File "gmail-python.py", line 101, in mainFunction
    mail = smtplib.SMTP('smtp.gmail.com', 465)
  File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 317, in connect
    (code, msg) = self.getreply()
  File "/usr/lib/python2.7/smtplib.py", line 368, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

试了这个 - smtplib.SMTP_SSL('smtp.gmail.com',465)并得到了这个错误

 raise SMTPException("STARTTLS extension not supported by server.")
smtplib.SMTPException: STARTTLS extension not supported by server.

删除server.ehlo()无效。 删除server.starttls()导致以下错误 -

raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsV\n5.7.14 3GHIcJvaksu56NrYm76aC0MqGI_sFI2cNaKSoFX6CV7TlyW593zn4dnJN7mG4TGI7edfcj\n5.7.14 l-MOV1Sb0Joyewnax8dxiL5BQ28Gg3UsWDfibI5TZ-tEaqqpL4WEzX5hsqYg6ZIQlczjyq\n5.7.14 UbElUx1C2E0QBt-X3uCuueb06uPBqehnwNV-hOlYFCgZpxRR9fdi-rO65oD5c1asYyGaTh\n5.7.14 1VRKgMYJp-o_uMhMjGEyJWKLLYX3A> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 r85sm13251579pfr.48 - gsmtp')

我的帐户中已启用安全性较低的应用访问权限。 ubuntu上的Python版本2.7.6。

1 个答案:

答案 0 :(得分:0)

在此处转换Gmail的安全功能后尝试一下。 这可能会阻止第三方应用程序出于安全目的访问Gmail

https://www.google.com/settings/security/lesssecureapps