簡體   English   中英

無法使用python使用Gmail服務器發送電子郵件

[英]Unable to send email using gmail server using python

我正在嘗試使用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。

啟用Gmail的“安全功能”后,請嘗試此處。 這可能會阻止第三方應用程序出於安全目的訪問Gmail

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM