如何利用python發(fā)送郵件
一、zmial發(fā)送郵件
zmial是第三方庫(kù),需進(jìn)行安裝
pip install zmail
完成后,來(lái)給發(fā)一封郵件
subject:標(biāo)題content_text:內(nèi)容
import zmail server = zmail.server(’發(fā)件人郵箱地址’,’授權(quán)碼’) server.send_mail(’收件人郵箱地址’,{’subject’:’Hello!’,’content_text’:’By zmail.’})
二、smtplib發(fā)送郵件
import smtplibfrom email.mime.text import MIMEText#--------發(fā)件相關(guān)參數(shù)--------smtpserver='smtp.qq.com' #連接服務(wù)器port = 465 #端口sender = '741841851@qq.com'#賬號(hào)psw = 'xxxxx'#密碼 授權(quán)碼receiver='741841851@qq.com'#接收人#--------編輯郵件內(nèi)容--------subject='qq郵件主題'body= ’<p>這個(gè)是發(fā)送的qq郵件</p>’msg = MIMEText(body,’html’,’utf-8’)msg[’from’]=sendermsg[’to’]=’741841851@qq.com’msg[’subject’]=subject#-----------test_email-------smtp = smtplib.SMTP_SSL(smtpserver,port)#連接服務(wù)器smtp.login(sender,psw)#登錄smtp.sendmail(sender,receiver,msg.as_string())#發(fā)送郵件smtp.quit()
三、發(fā)送帶附件的郵件
import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimport ossmtpserver=’smtp.qq.com’port =465sender=’741841851@qq.com’psw = ’xxxx’recevier = '741841851@qq.com'filenamepath = os.path.join(os.path.dirname(os.path.realpath(__file__)),’ceshi.html’)with open(filenamepath,’rb’) as f: mail_body=f.read().decode(’utf-8’)msg = MIMEMultipart()msg[’from’]=sender#發(fā)件人msg[’to’]=recevier#收件人msg[’subject’]=’這是我的主題99’#主題# 正文body = MIMEText(mail_body,’html’,’utf-8’)msg.attach(body)#附件att = MIMEText(mail_body,’base64’,’gbk’)#用utf-8會(huì)出現(xiàn)亂碼att[’Content-Type’]=’application/octet-stream’att[’Content-Disposition’]=’attachment;filename='test_report.html'’msg.attach(att)####發(fā)送郵件try: smtp = smtplib.SMTP() smtp.connect(smtpserver)#連接服務(wù)器 smtp.login(sender,psw)#登錄except: smtp = smtplib.SMTP_SSL(smtpserver,port) smtp.login(sender,psw)#登錄smtp.sendmail(sender,recevier,msg.as_string())#發(fā)送郵件smtp.quit()
以上就是如何利用python發(fā)送郵件的詳細(xì)內(nèi)容,更多關(guān)于python 發(fā)送郵件的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)2. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解3. uni-app結(jié)合.NET 7實(shí)現(xiàn)微信小程序訂閱消息推送4. jscript與vbscript 操作XML元素屬性的代碼5. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)6. .NET中實(shí)現(xiàn)對(duì)象數(shù)據(jù)映射示例詳解7. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)8. ASP.NET Core 7 Razor Pages項(xiàng)目發(fā)布到IIS的詳細(xì)過(guò)程9. XML在語(yǔ)音合成中的應(yīng)用10. 如何使用ASP.NET Core 配置文件
