用smtplib和email封裝python發(fā)送郵件模塊類(lèi)分享
#!/usr/bin/python
# encoding=utf-8
# Filename: send_email.py
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
class SendEmail:
# 構(gòu)造函數(shù):初始化基本信息
def __init__(self, host, user, passwd):
lInfo = user.split("@")
self._user = user
self._account = lInfo[0]
self._me = self._account + "<" + self._user + ">"
server = smtplib.SMTP()
server.connect(host)
server.login(self._account, passwd)
self._server = server
# 發(fā)送文件或html郵件
def sendTxtMail(self, to_list, sub, content, subtype='html'):
# 如果發(fā)送的是文本郵件,則_subtype設(shè)置為plain
# 如果發(fā)送的是html郵件,則_subtype設(shè)置為html
msg = MIMEText(content, _subtype=subtype, _charset='utf-8')
msg['Subject'] = sub
msg['From'] = self._me
msg['To'] = ";".join(to_list)
try:
self._server.sendmail(self._me, to_list, msg.as_string())
return True
except Exception, e:
print str(e)
return False
# 發(fā)送帶附件的文件或html郵件
def sendAttachMail(self, to_list, sub, content, subtype='html'):
# 創(chuàng)建一個(gè)帶附件的實(shí)例
msg = MIMEMultipart()
# 增加附件1
att1 = MIMEText(open(r'D:\javawork\PyTest\src\main.py','rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 這里的filename可以任意寫(xiě),寫(xiě)什么名字,郵件中顯示什么名字
att1["Content-Disposition"] = 'attachment; filename="main.py"'
msg.attach(att1)
# 增加附件2
att2 = MIMEText(open(r'D:\javawork\PyTest\src\main.py','rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="main.txt"'
msg.attach(att2)
# 增加郵件內(nèi)容
msg.attach(MIMEText(content, _subtype=subtype, _charset='utf-8'))
msg['Subject'] = sub
msg['From'] = self._me
msg['To'] = ";".join(to_list)
try:
self._server.sendmail(self._me, to_list, msg.as_string())
return True
except Exception, e:
print str(e)
return False
# 發(fā)送帶附件的文件或html郵件
def sendImageMail(self, to_list, sub, content, subtype='html'):
# 創(chuàng)建一個(gè)帶附件的實(shí)例
msg = MIMEMultipart()
# 增加郵件內(nèi)容
msg.attach(MIMEText(content, _subtype=subtype, _charset='utf-8'))
# 增加圖片附件
image = MIMEImage(open(r'D:\javawork\PyTest\src\test.jpg','rb').read())
#附件列表中顯示的文件名
image.add_header('Content-Disposition', 'attachment;filename=p.jpg')
msg.attach(image)
msg['Subject'] = sub
msg['From'] = self._me
msg['To'] = ";".join(to_list)
try:
self._server.sendmail(self._me, to_list, msg.as_string())
return True
except Exception, e:
print str(e)
return False
# 析構(gòu)函數(shù):釋放資源
def __del__(self):
self._server.quit()
self._server.close()
mailto_list = ['xxx@163.com']
mail = SendEmail('smtp.163.com', 'xxx@163.com', 'xxxxxx')
if mail.sendTxtMail(mailto_list, "測(cè)試郵件", "hello world!<br><br><h1>你好,發(fā)送文本文件測(cè)試<h1>"):
print "發(fā)送成功"
else:
print "發(fā)送失敗"
if mail.sendAttachMail(mailto_list, "測(cè)試郵件-帶兩個(gè)附件", "hello world!<br><br><h1>你好,發(fā)送文本文件測(cè)試<h1>"):
print "發(fā)送成功"
else:
print "發(fā)送失敗"
if mail.sendImageMail(mailto_list, "測(cè)試郵件-帶一個(gè)圖片的附件", "hello world!<br><br><h1>你好,發(fā)送文本文件測(cè)試<h1>"):
print "發(fā)送成功"
else:
print "發(fā)送失敗"
- python email smtplib模塊發(fā)送郵件代碼實(shí)例
- Python使用email模塊對(duì)郵件進(jìn)行編碼和解碼的實(shí)例教程
- python中使用smtplib和email模塊發(fā)送郵件實(shí)例
- python通過(guò)imaplib模塊讀取gmail里郵件的方法
- 使用Python3 poplib模塊刪除服務(wù)器多天前的郵件實(shí)現(xiàn)代碼
- Python使用poplib模塊和smtplib模塊收發(fā)電子郵件的教程
- 在Python中使用poplib模塊收取郵件的教程
- Python網(wǎng)絡(luò)編程之使用email、smtplib、poplib、imaplib模塊收發(fā)郵件
相關(guān)文章
Python?pandas修剪函數(shù)clip使用實(shí)例探究
在數(shù)據(jù)處理和分析中,經(jīng)常面臨著需要限制數(shù)據(jù)范圍的情況,而pandas庫(kù)提供的clip函數(shù)就是一個(gè)強(qiáng)大的工具,可以方便地對(duì)數(shù)據(jù)進(jìn)行修剪,本文將深入介紹clip函數(shù)的基本用法、常見(jiàn)參數(shù)以及實(shí)際場(chǎng)景中的應(yīng)用,以幫助大家充分理解并靈活運(yùn)用這一功能2024-01-01深入理解Python虛擬機(jī)中常見(jiàn)魔術(shù)方法的使用
本文主要給大家介紹在 python 當(dāng)中與數(shù)學(xué)計(jì)算相關(guān)的一些常見(jiàn)的魔術(shù)方法,是在很多科學(xué)計(jì)算的包當(dāng)中都使用到的魔術(shù)方法,感興趣的小伙伴可以了解一下2023-05-05利用Python寫(xiě)個(gè)摸魚(yú)監(jiān)控進(jìn)程
繼打游戲、看視頻等摸魚(yú)行為被監(jiān)控后,現(xiàn)在打工人離職的傾向也會(huì)被監(jiān)控。今天就帶大家領(lǐng)略一下怎么寫(xiě)幾行Python代碼,就能監(jiān)控電腦,感興趣的可以學(xué)習(xí)一下2022-02-02