日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python发邮件实例_python 发送邮件实例

發(fā)布時間:2025/3/15 python 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python发邮件实例_python 发送邮件实例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文本形式發(fā)郵件

#!/usr/bin/env?python3

#coding:?utf-8

importsmtplib

fromemail.mime.textimportMIMEText

fromemail.headerimportHeader

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形式發(fā)郵件

#!/usr/bin/env?python3

#coding:?utf-8

importsmtplib

fromemail.mime.textimportMIMEText

sender?=?'***'

receiver?=?'***'

subject?=?'python?email?test'

smtpserver?=?'smtp.163.com'

username?=?'***'

password?=?'***'

msg?=?MIMEText('

你好

','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形式發(fā)郵件

#!/usr/bin/env?python3

#coding:?utf-8

importsmtplib

fromemail.mime.multipartimportMIMEMultipart

fromemail.mime.textimportMIMEText

fromemail.mime.p_w_picpathimportMIMEImage

sender?=?'***'

receiver?=?'***'

subject?=?'python?email?test'

smtpserver?=?'smtp.163.com'

username?=?'***'

password?=?'***'

msgRoot?=?MIMEMultipart('related')

msgRoot['Subject']?='test?message'

msgText?=?MIMEText('Some?HTML?text?and?an?p_w_picpath.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','')

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

importsmtplib

fromemail.mime.multipartimportMIMEMultipart

fromemail.mime.textimportMIMEText

fromemail.mime.p_w_picpathimportMIMEImage

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"]?='p_w_upload;?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()

總結(jié)

以上是生活随笔為你收集整理的python发邮件实例_python 发送邮件实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。