亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2014年02月17日 10:06:07   作者:  
本文針對發(fā)郵件相關(guān)的操作進行了封裝,包括發(fā)送文本、HTML、帶附件的郵件,使用Python發(fā)郵件,主要用到smtplib和email兩個模塊,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

#!/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)建一個帶附件的實例
        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)

        # 增加郵件內(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)建一個帶附件的實例
        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, "測試郵件", "hello world!<br><br><h1>你好,發(fā)送文本文件測試<h1>"): 
    print "發(fā)送成功" 
else: 
    print "發(fā)送失敗"

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

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

相關(guān)文章

  • Python給定一個句子倒序輸出單詞以及字母的方法

    Python給定一個句子倒序輸出單詞以及字母的方法

    今天小編就為大家分享一篇Python給定一個句子倒序輸出單詞以及字母的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python?pandas修剪函數(shù)clip使用實例探究

    Python?pandas修剪函數(shù)clip使用實例探究

    在數(shù)據(jù)處理和分析中,經(jīng)常面臨著需要限制數(shù)據(jù)范圍的情況,而pandas庫提供的clip函數(shù)就是一個強大的工具,可以方便地對數(shù)據(jù)進行修剪,本文將深入介紹clip函數(shù)的基本用法、常見參數(shù)以及實際場景中的應(yīng)用,以幫助大家充分理解并靈活運用這一功能
    2024-01-01
  • 深入理解Python虛擬機中常見魔術(shù)方法的使用

    深入理解Python虛擬機中常見魔術(shù)方法的使用

    本文主要給大家介紹在 python 當中與數(shù)學計算相關(guān)的一些常見的魔術(shù)方法,是在很多科學計算的包當中都使用到的魔術(shù)方法,感興趣的小伙伴可以了解一下
    2023-05-05
  • python基礎(chǔ)入門之字典和集合

    python基礎(chǔ)入門之字典和集合

    Python中的字典和集合是非常相似的數(shù)據(jù)類型,字典是無序的鍵值對。集合中的數(shù)據(jù)是不重復(fù)的,并且不能通過索引去修改集合中的值,我們可以往集合中新增或者修改數(shù)據(jù)。集合是無序的,并且支持數(shù)學中的集合運算,例如并集和交集等。
    2021-06-06
  • Python中@符號的具體使用

    Python中@符號的具體使用

    本文主要介紹了Python中@符號的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • python實現(xiàn)發(fā)送郵件功能

    python實現(xiàn)發(fā)送郵件功能

    這篇文章主要為大家詳細介紹了python實現(xiàn)發(fā)送郵件功能,使用的模塊是smtplib、MIMEText,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 解決PyCharm控制臺輸出亂碼的問題

    解決PyCharm控制臺輸出亂碼的問題

    今天小編就為大家分享一篇解決PyCharm控制臺輸出亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python中圖像通用操作的實現(xiàn)代碼

    Python中圖像通用操作的實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了Python中圖像通用操作的實現(xiàn),例如:圖像旋轉(zhuǎn)、圖像縮放等,文中的示例代碼講解詳細,需要的可以參考一下
    2023-07-07
  • Python函數(shù)嵌套實例

    Python函數(shù)嵌套實例

    這篇文章主要介紹了Python函數(shù)嵌套實例,本文用實例講解了Python中的函數(shù)嵌套特性,需要的朋友可以參考下
    2014-09-09
  • 利用Python寫個摸魚監(jiān)控進程

    利用Python寫個摸魚監(jiān)控進程

    繼打游戲、看視頻等摸魚行為被監(jiān)控后,現(xiàn)在打工人離職的傾向也會被監(jiān)控。今天就帶大家領(lǐng)略一下怎么寫幾行Python代碼,就能監(jiān)控電腦,感興趣的可以學習一下
    2022-02-02

最新評論