新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
怎么在python3中使用SMTP發(fā)送一個(gè)HTML格式郵件-創(chuàng)新互聯(lián)
今天就跟大家聊聊有關(guān)怎么在python3中使用SMTP發(fā)送一個(gè)HTML格式郵件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
使用的環(huán)境為:
Python 3.6.3 (64bit)
PyCharm 2017.3 (64bit)
1.實(shí)現(xiàn)代碼:
import smtplib from email.header import Header from email.mime.text import MIMEText # 發(fā)件人和收件人 sender = '[email protected]' receiver = '[email protected]' # 所使用的用來發(fā)送郵件的SMTP服務(wù)器 smtpserver = 'smtp.163.com' # 發(fā)送郵箱的用戶名和授權(quán)碼(不是登錄郵箱的密碼) username = 'XXXXX' password = 'XXXXX' # 郵件主題 mail_title = '主題:測(cè)試報(bào)告3' # 讀取html文件內(nèi)容 f = open('report_test.html', 'rb') # HTML文件默認(rèn)和當(dāng)前文件在同一路徑下,若不在同一路徑下,需要指定要發(fā)送的HTML文件的路徑 mail_body = f.read() f.close() # 郵件內(nèi)容, 格式, 編碼 message = MIMEText(mail_body, 'html', 'utf-8') message['From'] = sender message['To'] = receiver message['Subject'] = Header(mail_title, 'utf-8') try: smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, message.as_string()) print("發(fā)送郵件成功?。?!") smtp.quit() except smtplib.SMTPException: print("發(fā)送郵件失敗?。。?)
看完上述內(nèi)容,你們對(duì)怎么在python3中使用SMTP發(fā)送一個(gè)HTML格式郵件有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
文章名稱:怎么在python3中使用SMTP發(fā)送一個(gè)HTML格式郵件-創(chuàng)新互聯(lián)
URL鏈接:http://fisionsoft.com.cn/article/dppgid.html