国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 泸州市| 宁夏| 永定县| 响水县| 大竹县| 克拉玛依市| 西城区| 嘉义县| 镇江市| 华池县| 金乡县| 阿图什市| 察隅县| 涡阳县| 电白县| 新丰县| 容城县| 报价| 阿拉善盟| 勐海县| 芦山县| 正定县| 吉木萨尔县| 保山市| 河西区| 朝阳区| 满洲里市| 屏东市| 肇州县| 峨眉山市| 濉溪县| 十堰市| 通城县| 红原县| 汉源县| 万安县| 拜城县| 柳河县| 拉萨市| 曲沃县| 定远县|