python使用QQ郵箱實現(xiàn)自動發(fā)送郵件
最近用到Python自動發(fā)送郵件,主要就是三步,登錄郵件、寫郵件內(nèi)容、發(fā)送,用到的庫是 smtplib 和 email,直接使用pip安裝即可
我使用的是QQ郵箱,首先需要設(shè)置QQ郵箱POP3/SMTP服務(wù)



記住這個授權(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ā)送成功')

二.發(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ā)送成功')

三.發(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ā)送成功')

四.發(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ā)送成功')

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php使用正則驗證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)2. ASP中實現(xiàn)字符部位類似.NET里String對象的PadLeft和PadRight函數(shù)3. Jsp中request的3個基礎(chǔ)實踐4. 淺談由position屬性引申的css進(jìn)階討論5. JSP+Servlet實現(xiàn)文件上傳到服務(wù)器功能6. vue前端RSA加密java后端解密的方法實現(xiàn)7. 利用CSS3新特性創(chuàng)建透明邊框三角8. 解析原生JS getComputedStyle9. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享10. CSS可以做的幾個令你嘆為觀止的實例分享

網(wǎng)公網(wǎng)安備