国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

用smtplib和email封裝python發送郵件模塊類分享

2019-11-25 18:31:02
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

#!/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:
    # 構造函數:初始化基本信息
    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     

    # 發送文件或html郵件   
    def sendTxtMail(self, to_list, sub, content, subtype='html'):   
        # 如果發送的是文本郵件,則_subtype設置為plain
        # 如果發送的是html郵件,則_subtype設置為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

    # 發送帶附件的文件或html郵件      
    def sendAttachMail(self, to_list, sub, content, subtype='html'):
        # 創建一個帶附件的實例
        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可以任意寫,寫什么名字,郵件中顯示什么名字
        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)

        # 增加郵件內容
        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
     # 發送帶附件的文件或html郵件      
    def sendImageMail(self, to_list, sub, content, subtype='html'):
        # 創建一個帶附件的實例
        msg = MIMEMultipart()

        # 增加郵件內容
        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

    # 析構函數:釋放資源 
    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, "測試郵件", "hello world!<br><br><h1>你好,發送文本文件測試<h1>"): 
    print "發送成功" 
else: 
    print "發送失敗"

if mail.sendAttachMail(mailto_list, "測試郵件-帶兩個附件", "hello world!<br><br><h1>你好,發送文本文件測試<h1>"): 
    print "發送成功" 
else: 
    print "發送失敗"

if mail.sendImageMail(mailto_list, "測試郵件-帶一個圖片的附件", "hello world!<br><br><h1>你好,發送文本文件測試<h1>"): 
    print "發送成功" 
else: 
    print "發送失敗"

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西平县| 乌兰察布市| 梧州市| 上林县| 宁海县| 呼伦贝尔市| 溧阳市| 蒲江县| 宁武县| 盘锦市| 宁国市| 江达县| 盐池县| 合江县| 林西县| 宣武区| 麟游县| 郧西县| 涿鹿县| 岳西县| 新闻| 若尔盖县| 项城市| 枣强县| 承德市| 侯马市| 连江县| 峡江县| 正宁县| 安塞县| 新丰县| 老河口市| 开江县| 青龙| 尤溪县| 石渠县| 专栏| 顺义区| 大同县| 根河市| 巨鹿县|