国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

python使用QQ郵箱實現自動發送郵件

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

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

我使用的是QQ郵箱,首先需要設置QQ郵箱POP3/SMTP服務

python使用QQ郵箱實現自動發送郵件

python使用QQ郵箱實現自動發送郵件

python使用QQ郵箱實現自動發送郵件

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

一.發送普通文本郵件

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

python使用QQ郵箱實現自動發送郵件

二.發送攜帶附件的郵件

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

python使用QQ郵箱實現自動發送郵件

三.發送攜帶圖片的附件

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

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

python使用QQ郵箱實現自動發送郵件

四.發送 html 格式的郵件

import smtplibfrom email.mime.text import MIMEText#發送多種類型的郵件from email.mime.multipart import MIMEMultipartimport datetimemsg_from = ’[email protected]’ # 發送方郵箱passwd = ’xxxxxx’ to= [’[email protected]’] #接受方郵箱 #設置郵件內容#MIMEMultipart類可以放任何內容msg = MIMEMultipart()# conntent='這個是字符串'# #把內容加進去# msg.attach(MIMEText(conntent,’plain’,’utf-8’)) #添加附件att1=MIMEText(open(’result.xlsx’,’rb’).read(),’base64’,’utf-8’) #打開附件att1[’Content-Type’]=’application/octet-stream’ #設置類型是流媒體格式att1[’Content-Disposition’]=’attachment;filename=result.xlsx’ #設置描述信息 att2=MIMEText(open(’1.jpg’,’rb’).read(),’base64’,’utf-8’)att2[’Content-Type’]=’application/octet-stream’ #設置類型是流媒體格式att2[’Content-Disposition’]=’attachment;filename=1.jpg’ #設置描述信息 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='牛頓'#構造HTMLcontent = ’’’<html><body> <h1 align='center'>這個是標題,xxxx通知</h1> <p><strong>您好:</strong></p> <blockquote><p><strong>以下內容是本次會議的紀要,請查收!</strong></p></blockquote> <blockquote><p><strong>發言人:{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’)) #設置郵件主題msg[’Subject’]='這個是郵件主題' #發送方信息msg[’From’]=msg_from #開始發送 #通過SSL方式發送,服務器地址和端口s = smtplib.SMTP_SSL('smtp.qq.com', 465)# 登錄郵箱s.login(msg_from, passwd)#開始發送s.sendmail(msg_from,to,msg.as_string())print('郵件發送成功')

python使用QQ郵箱實現自動發送郵件

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: qq Python
相關文章:
主站蜘蛛池模板: 亚洲精品综合欧美一区二区三区 | 国产精品亚洲片在线不卡 | 欧美亚洲另类视频 | 国产黄色在线播放 | 久久精品国产欧美日韩亚洲 | 色咪味成人网 | 国产色视频一区二区三区 | 国产成人精品免费视频大全五级 | 日本一区二区三区精品视频 | 一本色道久久综合亚洲精品高清 | 亚洲日韩中文字幕在线播放 | 亚洲精品中文字幕久久久久久 | 欧美成人亚洲综合精品欧美激情 | 精品久久久久久 | 在线视频久 | 久久亚洲精品中文字幕 | 国产免费一区二区三区在线观看 | 免费一级做a爰片性色毛片 免费一极毛片 | 国产国产成人精品久久 | 国产91美女| 欧美一欧美一级毛片 | 国产精品欧美一区二区在线看 | 香港免费毛片 | 久草视频中文在线 | 美女三级黄 | 成年午夜性视频免费播放 | 亚洲美女性生活视频 | 国产伦精品一区二区三区四区 | 午夜在线社区视频 | 国产高清在线看 | 久久草在线免费 | 国产欧美一区二区精品性色 | www亚洲视频 | 免费的特黄特色大片在线观看 | 免费在线视频成人 | 欧美特黄一级aa毛片 | 一区二区不卡在线 | 国产杨幂福利在线视频观看 | 欧美另类videosbestsex高清 | 国产不卡影院 | 夜夜爽影院 |