Python實(shí)現(xiàn)發(fā)送郵件到自己郵箱
1、緣由
在日常開發(fā)中,我們經(jīng)常需要監(jiān)控應(yīng)用程序的狀態(tài),及時(shí)發(fā)現(xiàn)問題并采取措施解決。而通過郵件發(fā)送報(bào)警信息則是一種常見的實(shí)現(xiàn)方式。
Python提供了許多內(nèi)置的庫和第三方庫來方便我們發(fā)送郵件。在本文中,我將介紹如何使用Python發(fā)送郵件,以qq郵箱為例,來實(shí)現(xiàn)應(yīng)用程序監(jiān)控報(bào)警的功能,包括設(shè)置SMTP服務(wù)器、編寫郵件內(nèi)容以及添加附件等操作。如果你想學(xué)習(xí)如何使用Python發(fā)送郵件,并將其應(yīng)用于應(yīng)用程序監(jiān)控報(bào)警,那么這篇文章就是為你準(zhǔn)備的
2、設(shè)置SMTP服務(wù)器
登錄到QQ郵箱后臺(tái)然后點(diǎn)擊賬戶
找到“POP3/SMTP服務(wù)”和“IMAP/SMTP服務(wù)”項(xiàng),點(diǎn)“開啟”。

成功開啟后可以看到授權(quán)碼,注意保存這個(gè)授權(quán)碼

使用SSL的通用配置如下:
接收郵件服務(wù)器:pop.qq.com,使用SSL,端口號(hào)995
發(fā)送郵件服務(wù)器:smtp.qq.com,使用SSL,端口號(hào)465或587
賬戶名:您的QQ郵箱賬戶名(如果您是VIP帳號(hào)或Foxmail帳號(hào),賬戶名需要填寫完整的郵件地址)
密碼:您的QQ郵箱密碼
電子郵件地址:您的QQ郵箱的完整郵件地址
3、使用python發(fā)送
先看源碼
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# 發(fā)件人信息
sender_email = "657029702@qq.com"
sender_password = "上文申請(qǐng)的授權(quán)碼"
# 收件人信息
recipient_email = "493614550@qq.com"
# 構(gòu)造郵件對(duì)象
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Subject'] = "這是一封測試郵件"
# 添加正文
body = "這是一封測試郵件,用Python發(fā)送。"
msg.attach(MIMEText(body, 'plain'))
# 添加附件
with open("example.pdf", "rb") as attachment:
part = MIMEApplication(attachment.read(), _subtype='pdf')
part.add_header('Content-Disposition', 'attachment', filename="example.pdf")
msg.attach(part)
# 發(fā)送郵件
with smtplib.SMTP_SSL('smtp.qq.com', 465) as smtp:
smtp.login(sender_email, sender_password)
smtp.sendmail(sender_email, recipient_email, msg.as_string())這里使用了三方庫smtplib,安裝一下就行
這里加了一個(gè)附件,將example.pdf放到和腳本同一目錄下

完美!
4、總結(jié)
在本文中,我們學(xué)習(xí)了如何使用Python來發(fā)送郵件,并且將其應(yīng)用于應(yīng)用程序監(jiān)控報(bào)警。通過發(fā)送郵件,我們可以及時(shí)發(fā)現(xiàn)應(yīng)用程序中的問題,并采取措施解決,從而提高系統(tǒng)的穩(wěn)定性和可靠性。
同時(shí),需要注意的是,在實(shí)際應(yīng)用中,我們需要注意郵件發(fā)送的頻率和內(nèi)容,以避免對(duì)接收方造成騷擾和困擾。
5、補(bǔ)充
除了簡單的發(fā)送郵件到郵箱,Python還可以實(shí)現(xiàn)定時(shí)發(fā)送郵件,下面是實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助
python自動(dòng)批量發(fā)郵件腳本
'''''
該模塊使自動(dòng)發(fā)送郵件的模塊
模塊初始化時(shí)需要設(shè)置:
sender:發(fā)送人
reciver:接收者
smtpServer:發(fā)送人的服務(wù)器類型
password:登錄命令
subject:郵件標(biāo)題
datafile:數(shù)據(jù)文件
文件包含六個(gè)函數(shù):
senderLogin():連接服務(wù)并登錄服務(wù)
setSubject():設(shè)置郵件標(biāo)題
SendMessage():郵件發(fā)送的信息
sendMail():發(fā)送郵件
quitMail():關(guān)閉郵件服務(wù)
run():執(zhí)行登錄、設(shè)置郵件標(biāo)題、設(shè)置郵件發(fā)送信息、發(fā)送郵件、關(guān)閉郵件服務(wù)
'''
import smtplib
from mangerResultFile import FileManger
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class AutoMail(object):
def __init__(self,sender,reciver,smtpServer, password,subject,datafile):
#設(shè)置發(fā)送人
self.sender=sender
#設(shè)置登錄密碼
self.password=password
#設(shè)置接收者
self.reciver=reciver
#設(shè)置郵件標(biāo)題
self.subject=subject
#設(shè)置附件路徑
self.datafile=datafile
#設(shè)置發(fā)送郵件服務(wù)
self.smtpServer=smtpServer
#創(chuàng)建一個(gè)smtp實(shí)例
self.smtp = smtplib.SMTP()
#設(shè)置下發(fā)送信息包含的類型的信息體
self.msgRoot =MIMEMultipart('related')
#調(diào)用run函數(shù)運(yùn)行
self.run()
#發(fā)送用戶登錄
def senderLogin(self):
#通過smtp實(shí)例的connect方法連接發(fā)送郵件服務(wù)
self.smtp.connect(self.smtpServer)
#通過smtp實(shí)例的login方法登錄發(fā)送郵件服務(wù)
self.smtp.login(self.sender,self.password)
def setSubject(self):
#設(shè)置郵件標(biāo)題
self.msgRoot['Subject']=self.subject
def SendMessage(self):
#讀取附件信息到att中
att =MIMEText(open( self.datafile, 'rb').read(), 'base64', 'utf8')
#設(shè)置att的內(nèi)容類型
att["Content-Type"]= 'application/octet-stream'
#給附件設(shè)置一個(gè)文件名
att["Content-Disposition"]= 'attachment; '+'filename='+FileManger().getLastFile()+''
self.msgRoot.attach(att)
def sendMail(self):
#發(fā)送郵件
self.smtp.sendmail(self.sender,self.reciver,self.msgRoot .as_string())
def quitMail(self):
#退出郵件服務(wù)
self.smtp.quit()
def run(self):
try:
self.senderLogin()
self.setSubject()
self.SendMessage()
self.sendMail()
self.quitMail()
print "send success...."
except Exception,e:
print e
def test():
#創(chuàng)建一個(gè)FileManger實(shí)例
fm=FileManger()
sender ='wang@163.com'
receiver ='e.wang@163.com'
smtpserver ='smtp.163.com'
password =' '
ject= 'XQL Autom excut project'
filpath=fm.getLastFileWithPath()
try:
AutoMail(sender,receiver,smtpserver,password,ject,filpath)
except Exception,e:
print e
if __name__=="__main__":
test()到此這篇關(guān)于Python實(shí)現(xiàn)發(fā)送郵件到自己郵箱的文章就介紹到這了,更多相關(guān)Python發(fā)送郵件到郵箱內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 邊緣擴(kuò)充方式的實(shí)現(xiàn)示例
本文主要介紹了python 邊緣擴(kuò)充方式的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
keras的siamese(孿生網(wǎng)絡(luò))實(shí)現(xiàn)案例
這篇文章主要介紹了keras的siamese(孿生網(wǎng)絡(luò))實(shí)現(xiàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
使用python寫一個(gè)自動(dòng)瀏覽文章的腳本實(shí)例
今天小編就為大家分享一篇使用python寫一個(gè)自動(dòng)瀏覽文章的腳本實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12

