2017-04-26 50 views
0
的smtplib

我試圖使用Python的模塊的smtplib發送一封電子郵件,但我不斷收到一個TimeoutError發送電子郵件。TimeoutError嘗試與

import smtplib 

def send(): 
    try: 
     server = smtplib.SMTP('smtp.gmail.com', 587) 
     server.ehlo() 
     server.starttls() 
     server.login(email, password) # Email and password are already defined 
     print('Enter recipient: ') 
     recipient = input() 
     print('Enter subject:') 
     subject = input() + '\n' 
     print('Enter body:') 
     body = input() 
     server.sendmail(email, recipient, body) 
     server.quit() 

    except TimeoutError as e: 
     print (str(e)) 

此代碼臨危一:

[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

+0

也許連接問題或防火牆? – jpnkls

回答

1

看起來沒有啓用「允許不夠安全的應用」在您的Gmail帳戶選項。

這裏有更正式從谷歌, https://support.google.com/accounts/answer/6010255?hl=en

+0

我已啓用此設置,但我仍未連接。我想如果試圖增加我的超時範圍:導入套接字 socket.setdefaulttimeout(120)這可能工作嗎? – user7722705

+0

@ user7722705:不要只問,試試吧! – martineau