国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python使用QQ郵箱實(shí)現(xiàn)自動發(fā)送郵件

瀏覽:67日期:2022-07-19 17:29:34

最近用到Python自動發(fā)送郵件,主要就是三步,登錄郵件、寫郵件內(nèi)容、發(fā)送,用到的庫是 smtplib 和 email,直接使用pip安裝即可

我使用的是QQ郵箱,首先需要設(shè)置QQ郵箱POP3/SMTP服務(wù)

python使用QQ郵箱實(shí)現(xiàn)自動發(fā)送郵件

python使用QQ郵箱實(shí)現(xiàn)自動發(fā)送郵件

python使用QQ郵箱實(shí)現(xiàn)自動發(fā)送郵件

記住這個授權(quán)碼,這個授權(quán)碼就是Python腳本中登錄郵箱時的密碼,而不是你平時登錄郵箱時的那個密碼

一.發(fā)送普通文本郵件

#發(fā)送多種類型的郵件from email.mime.multipart import MIMEMultipart msg_from = ’1508691067@qq.com’ # 發(fā)送方郵箱passwd = ’xxx’ #就是上面的授權(quán)碼 to= [’1508691067@qq.com’] #接受方郵箱 #設(shè)置郵件內(nèi)容#MIMEMultipart類可以放任何內(nèi)容msg = MIMEMultipart()conntent='這個是字符串'#把內(nèi)容加進(jìn)去msg.attach(MIMEText(conntent,’plain’,’utf-8’)) #設(shè)置郵件主題msg[’Subject’]='這個是郵件主題' #發(fā)送方信息msg[’From’]=msg_from #開始發(fā)送 #通過SSL方式發(fā)送,服務(wù)器地址和端口s = smtplib.SMTP_SSL('smtp.qq.com', 465)# 登錄郵箱s.login(msg_from, passwd)#開始發(fā)送s.sendmail(msg_from,to,msg.as_string())print('郵件發(fā)送成功')

python使用QQ郵箱實(shí)現(xiàn)自動發(fā)送郵件

二.發(fā)送攜帶附件的郵件

import smtplibfrom email.mime.text import MIMEText#發(fā)送多種類型的郵件from email.mime.multipart import MIMEMultipart msg_from = ’1508691067@qq.com’ # 發(fā)送方郵箱passwd = ’xxxxx’ to= [’1508691067@qq.com’] #接受方郵箱 #設(shè)置郵件內(nèi)容#MIMEMultipart類可以放任何內(nèi)容msg = MIMEMultipart()conntent='這個是字符串'#把內(nèi)容加進(jìn)去msg.attach(MIMEText(conntent,’plain’,’utf-8’)) #添加附件att1=MIMEText(open(’result.xlsx’,’rb’).read(),’base64’,’utf-8’) #打開附件att1[’Content-Type’]=’application/octet-stream’ #設(shè)置類型是流媒體格式att1[’Content-Disposition’]=’attachment;filename=result.xlsx’ #設(shè)置描述信息 msg.attach(att1) #加入到郵件中 #設(shè)置郵件主題msg[’Subject’]='這個是郵件主題' #發(fā)送方信息msg[’From’]=msg_from #開始發(fā)送 #通過SSL方式發(fā)送,服務(wù)器地址和端口s = smtplib.SMTP_SSL('smtp.qq.com', 465)# 登錄郵箱s.login(msg_from, passwd)#開始發(fā)送s.sendmail(msg_from,to,msg.as_string())print('郵件發(fā)送成功')

python使用QQ郵箱實(shí)現(xiàn)自動發(fā)送郵件

三.發(fā)送攜帶圖片的附件

同理,可以使用上面的方法也可以發(fā)送圖片附件

import smtplibfrom email.mime.text import MIMEText#發(fā)送多種類型的郵件from email.mime.multipart import MIMEMultipart msg_from = ’1508691067@qq.com’ # 發(fā)送方郵箱passwd = ’xxxxx’ to= [’1508691067@qq.com’] #接受方郵箱 #設(shè)置郵件內(nèi)容#MIMEMultipart類可以放任何內(nèi)容msg = MIMEMultipart()conntent='這個是字符串'#把內(nèi)容加進(jìn)去msg.attach(MIMEText(conntent,’plain’,’utf-8’)) #添加附件att1=MIMEText(open(’result.xlsx’,’rb’).read(),’base64’,’utf-8’) #打開附件att1[’Content-Type’]=’application/octet-stream’ #設(shè)置類型是流媒體格式att1[’Content-Disposition’]=’attachment;filename=result.xlsx’ #設(shè)置描述信息 att2=MIMEText(open(’1.jpg’,’rb’).read(),’base64’,’utf-8’)att2[’Content-Type’]=’application/octet-stream’ #設(shè)置類型是流媒體格式att2[’Content-Disposition’]=’attachment;filename=1.jpg’ #設(shè)置描述信息 msg.attach(att1) #加入到郵件中msg.attach(att2) #設(shè)置郵件主題msg[’Subject’]='這個是郵件主題' #發(fā)送方信息msg[’From’]=msg_from #開始發(fā)送 #通過SSL方式發(fā)送,服務(wù)器地址和端口s = smtplib.SMTP_SSL('smtp.qq.com', 465)# 登錄郵箱s.login(msg_from, passwd)#開始發(fā)送s.sendmail(msg_from,to,msg.as_string())print('郵件發(fā)送成功')

python使用QQ郵箱實(shí)現(xiàn)自動發(fā)送郵件

四.發(fā)送 html 格式的郵件

import smtplibfrom email.mime.text import MIMEText#發(fā)送多種類型的郵件from email.mime.multipart import MIMEMultipartimport datetimemsg_from = ’1508691067@qq.com’ # 發(fā)送方郵箱passwd = ’xxxxxx’ to= [’1508691067@qq.com’] #接受方郵箱 #設(shè)置郵件內(nèi)容#MIMEMultipart類可以放任何內(nèi)容msg = MIMEMultipart()# conntent='這個是字符串'# #把內(nèi)容加進(jìn)去# msg.attach(MIMEText(conntent,’plain’,’utf-8’)) #添加附件att1=MIMEText(open(’result.xlsx’,’rb’).read(),’base64’,’utf-8’) #打開附件att1[’Content-Type’]=’application/octet-stream’ #設(shè)置類型是流媒體格式att1[’Content-Disposition’]=’attachment;filename=result.xlsx’ #設(shè)置描述信息 att2=MIMEText(open(’1.jpg’,’rb’).read(),’base64’,’utf-8’)att2[’Content-Type’]=’application/octet-stream’ #設(shè)置類型是流媒體格式att2[’Content-Disposition’]=’attachment;filename=1.jpg’ #設(shè)置描述信息 msg.attach(att1) #加入到郵件中msg.attach(att2) now_time = datetime.datetime.now()year = now_time.yearmonth = now_time.monthday = now_time.daymytime = str(year) + ' 年 ' + str(month) + ' 月 ' + str(day) + ' 日 'fayanren='愛因斯坦'zhuchiren='牛頓'#構(gòu)造HTMLcontent = ’’’<html><body> <h1 align='center'>這個是標(biāo)題,xxxx通知</h1> <p><strong>您好:</strong></p> <blockquote><p><strong>以下內(nèi)容是本次會議的紀(jì)要,請查收!</strong></p></blockquote> <blockquote><p><strong>發(fā)言人:{fayanren}</strong></p></blockquote> <blockquote><p><strong>主持人:{zhuchiren}</strong></p></blockquote> <p align='right'>{mytime}</p><body><html>’’’.format(fayanren=fayanren, zhuchiren=zhuchiren, mytime=mytime) msg.attach(MIMEText(content,’html’,’utf-8’)) #設(shè)置郵件主題msg[’Subject’]='這個是郵件主題' #發(fā)送方信息msg[’From’]=msg_from #開始發(fā)送 #通過SSL方式發(fā)送,服務(wù)器地址和端口s = smtplib.SMTP_SSL('smtp.qq.com', 465)# 登錄郵箱s.login(msg_from, passwd)#開始發(fā)送s.sendmail(msg_from,to,msg.as_string())print('郵件發(fā)送成功')

python使用QQ郵箱實(shí)現(xiàn)自動發(fā)送郵件

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: qq Python
相關(guān)文章:
主站蜘蛛池模板: 襄垣县| 亳州市| 阿克苏市| 太谷县| 大洼县| 瑞安市| 图们市| 上蔡县| 长宁区| 阳原县| 宣汉县| 伊川县| 乌拉特后旗| 拉萨市| 宜丰县| 丰镇市| 沐川县| 潜山县| 云南省| 浦东新区| 麻阳| 合山市| 遵义市| 荣昌县| 德令哈市| 元江| 茂名市| 合川市| 安庆市| 浦江县| 维西| 和田县| 永康市| 洛扎县| 招远市| 宁夏| 双峰县| 山西省| 富民县| 平凉市| 阳曲县|