-1
我正在學習python 3.6.0,我試圖創建一個電子郵件客戶端。 我從書中複製了代碼,但它不起作用。 我得到這個錯誤:無法創建與谷歌服務器的SMTP會話
「TimeoutError:WinError 10060]連接嘗試失敗,因爲連接的方沒有正確一段時間後響應或已建立connectioñ失敗,因爲連接的主機沒有反應」
這是我的代碼:
#!/usr/bin/env python3
# Listing 2
import getpass
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587 # ssl port 465, tls port 587
def send_email(sender, recipient):
""" Send email message """
msg = MIMEMultipart()
msg['To'] = recipient
msg['From'] = sender
msg['Subject'] = input('Enter your email subject: ')
message = input('Enter your email message. Press Enter when finished. ')
part = MIMEText('text', "plain")
part.set_payload(message)
msg.attach(part)
# create smtp session
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.set_debuglevel(1)
session.ehlo()
session.starttls()
session.ehlo
password = getpass.getpass(prompt="Enter you email password:")
# login to server
session.login(sender, password)
# send mail
session.sendmail(sender, recipient, msg.as_string())
print("You email is sent to {0}.".format(recipient))
session.quit()
if __name__ == '__main__':
sender = input("Enter sender email address: ")
recipient = input("Enter recipeint email address: ")
send_email(sender, recipient)
任何人可以幫助?我錯在哪裏? 在此先感謝:)
謝謝。我的連接似乎工作,我禁用防火牆,但仍然有這個錯誤。將繼續調查... – Matt