国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python郵件發(fā)送smtplib使用詳解

2020-02-23 00:11:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了python郵件發(fā)送smtplib使用具體代碼,供大家參考,具體內(nèi)容如下

文件形式的郵件

#!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header  sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***'  msg = MIMEText('你好','text','utf-8')#中文需參數(shù)‘utf-8',單字節(jié)字符不需要 msg['Subject'] = Header(subject, 'utf-8')  smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() 

HTML形式的郵件

#!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText  sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***'  msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8')  msg['Subject'] = subject  smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() 

帶圖片的HTML郵件

#!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage  sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***'  msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'test message'  msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8') msgRoot.attach(msgText)  fp = open('h://python//1.jpg', 'rb') msgImage = MIMEImage(fp.read()) fp.close()  msgImage.add_header('Content-ID', '<image1>') msgRoot.attach(msgImage)  smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msgRoot.as_string()) smtp.quit() 

帶附件的郵件

#!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage  sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***'  msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'test message'  #構(gòu)造附件 att = MIMEText(open('h://python//1.jpg', 'rb').read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="1.jpg"' msgRoot.attach(att)    smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msgRoot.as_string()) smtp.quit() 

群發(fā)郵件

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 唐山市| 乃东县| 九寨沟县| 迁西县| 东乌| 六枝特区| 句容市| 沐川县| 岐山县| 镇沅| 新晃| 江门市| 金沙县| 罗定市| 浙江省| 花莲县| 吉隆县| 云南省| 萝北县| 伊宁县| 获嘉县| 武川县| 南雄市| 萨嘎县| 银川市| 枣强县| 永清县| 莲花县| 内黄县| 福鼎市| 诸城市| 德安县| 康乐县| 望奎县| 鄂托克旗| 金平| 七台河市| 铜鼓县| 陇川县| 陇川县| 美姑县|