2017-01-30 53 views
0

我寫了一個簡單的腳本通過Gmail SMTP服務器發送電子郵件。這裏的代碼:Python smtplib:Gmail的作品,但只有「有時」

import smtplib 
msg = 'Hello World!' 
server = smtplib.SMTP('smtp.gmail.com', 587) #port 465 or 587 
server.ehlo() 
server.starttls() 
server.ehlo() 
server.login('[email protected]','password') 
server.sendmail('[email protected]','[email protected]', msg) 
server.close() 

有時它有效,有時我得到一個錯誤。這是什麼原因?我已將Gmail配置爲接受安全性較低的應用程序。如果發生錯誤,我會收到以下消息:

Traceback (most recent call last): 
    File "email.py", line 31, in <module> 
    server.login('[email protected]','password') 
    File "/usr/lib/python3.5/smtplib.py", line 729, in login 
    raise last_exception 
    File "/usr/lib/python3.5/smtplib.py", line 720, in login 
    initial_response_ok=initial_response_ok) 
    File "/usr/lib/python3.5/smtplib.py", line 641, in auth 
    raise SMTPAuthenticationError(code, resp) 
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvQ\n5.7.14 DBMYWMukfjghdjfkghfjkhjkfhgjkdhgdfjkghekjghekjgndjkSm5lAOfEpP2Nt\n5.7.14 QihtNp5izjfghjjkjhgbhjbGHJVHJVjhvhjbhjbhjDZwhJFV-FiyvI-OGW\n5.7.14 jcpmHcQAcOR8e8G0zOfdugjhfdjd-sdfiugdsjfdsrthdfewrzjhg-shgv2HxmgWpZg3Z\n5.7.14 4G1ENiAlgiEnrkXyRbTG3frjZZdPg> 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 g40sm24698383wrg.19 - gsmtp') 

我在因特網上找不到任何幫助。非常感謝每一個建議。

+0

如果您仔細閱讀錯誤消息,您會發現它記錄了此Google支持文章:https://support.google.com/mail/answer/78754。 – grooveplex

回答