亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Python 3 通過 SMTP 發送郵件時出現 TimeoutError

使用 Python 3 通過 SMTP 發送郵件時出現 TimeoutError

海綿寶寶撒 2023-07-05 15:29:39
我使用的是 Python3.7,運行代碼時出現以下錯誤:TimeoutError: [WinError 10060] 連接嘗試失敗,因為連接方在一段時間后沒有正確響應,或者由于連接的主機未能響應而建立的連接失敗這是我的代碼:import smtplibfrom email import encodersfrom email.mime.text import MIMETextfrom email.mime.base import MIMEBasefrom email.mime.multipart import MIMEMultipartserver = smtplib.SMTP('smtp.gmail.com', 25)server.connect("smtp.gmail.com",465)server.ehlo()with open('password.txt', 'r') as f:   password = f.read()server.login('[email protected]', password)msg = MIMEMultipart()msg['From'] = '[email protected]'msg['To'] = '[email protected]'msg['Subject'] = 'Mail testing with Python'with open('message.txt', 'r') as f:    message = f.read()msg.attach(MIMEText(message, 'plain'))text = msg.as_string()server.sendmail('[email protected]', '[email protected]', text)問題是什么?我的代碼有錯誤還是只是我的網絡連接有錯誤?
查看完整描述

1 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

首先,為了使 gmail 能夠與 python 腳本正常工作,您需要配置 gmail 以允許低安全性應用程序。我假設你已經完成了這一部分,否則你可以查看 gmail 幫助。

Gmail 幫助開啟安全性較低的應用

這是應該發送電子郵件的腳本:

import smtplib


from email.mime.text import MIMEText


from email.mime.multipart import MIMEMultipart


server = smtplib.SMTP_SSL('smtp.gmail.com', 465)


server.ehlo()


server.login('[email protected]', "your password")



msg = MIMEMultipart('alternative',)

msg['From'] = '[email protected]'

msg['To'] = '[email protected]'

msg['Subject'] = 'Mail testing with Python'


with open('message.txt', 'r') as f:

? ? message = f.read()


msg.attach(MIMEText(message, 'plain'))


text = msg.as_string().encode('utf8')


server.sendmail('[email protected]', '[email protected]', text)


查看完整回答
反對 回復 2023-07-05
  • 1 回答
  • 0 關注
  • 448 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號