如何利用python發(fā)送郵件
更新時間:2020年09月26日 14:15:36 作者:莫使嬌軀空對月
這篇文章主要介紹了如何利用python發(fā)送郵件,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
一、zmial發(fā)送郵件
zmial是第三方庫,需進行安裝
pip install zmail
完成后,來給發(fā)一封郵件
subject:標題
content_text:內(nèi)容
import zmail
server = zmail.server('發(fā)件人郵箱地址','授權(quán)碼')
server.send_mail('收件人郵箱地址',{'subject':'Hello!','content_text':'By zmail.'})
二、smtplib發(fā)送郵件
import smtplib from email.mime.text import MIMEText #--------發(fā)件相關(guān)參數(shù)-------- smtpserver="smtp.qq.com" #連接服務(wù)器 port = 465 #端口 sender = "741841851@qq.com"#賬號 psw = "xxxxx"#密碼 授權(quán)碼 receiver="741841851@qq.com"#接收人 #--------編輯郵件內(nèi)容-------- subject="qq郵件主題" body= '<p>這個是發(fā)送的qq郵件</p>' msg = MIMEText(body,'html','utf-8') msg['from']=sender msg['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 smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
smtpserver='smtp.qq.com'
port =465
sender='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會出現(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ā)送郵件的詳細內(nèi)容,更多關(guān)于python 發(fā)送郵件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python中的torch常用tensor處理函數(shù)示例詳解
這篇文章主要介紹了python中的torch常用tensor處理函數(shù),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
pytorch 指定gpu訓(xùn)練與多gpu并行訓(xùn)練示例
今天小編就為大家分享一篇pytorch 指定gpu訓(xùn)練與多gpu并行訓(xùn)練示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python協(xié)程的實現(xiàn)方式小結(jié)
協(xié)程是Python中強大的并發(fā)編程工具,允許開發(fā)者編寫異步代碼以提高程序的性能和效率,在本文中,我們將深入探討Python中協(xié)程的實現(xiàn)方式,包括生成器、asyncio庫和async/await關(guān)鍵字,我們還會提供詳細的示例代碼,幫助您理解和應(yīng)用協(xié)程,需要的朋友可以參考下2023-11-11
python實現(xiàn)emoji對齊特殊字符對齊高級文本對齊
這篇文章主要為大家介紹了python實現(xiàn)emoji對齊特殊字符對齊高級文本對齊方法實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11

