天天看點

Python 發送郵件

Python 發送郵件

#coding=utf-8
__author__ = 'Xue'
'''
郵件發送
version 1.0
'''

import smtplib
from email.mime.text import MIMEText

class xmail():
    mail_host=''
    mail_from=''
    mail_user=''
    mail_pwd=''
    def __init__(self,host,from_addr,user,pwd):
        self.mail_host=host
        self.mail_from=from_addr
        self.mail_user=user
        self.mail_pwd=pwd

    #收件人清單
    #标題
    #内容
    def send(self,to_list,title,content):
        msg=MIMEText(content,_subtype='html',_charset='gb2312')#設定為html格式
        msg['Subject']=title
        msg['From']=self.mail_from
        msg['To']=';'.join(to_list)
        try:
            s=smtplib.SMTP()
            s.connect(self.mail_host) #連接配接SMTP
            s.login(self.mail_user,self.mail_pwd)#登陸
            s.sendmail(self.mail_from,to_list,msg.as_string())
            s.close()
            return True
        except Exception, e:
            return False

if __name__=="__main__":
    #注意,内容前要加u,否則中文會亂碼
    cont=u'''
    經測試,本内容真實有效!絕對是真的!
    '''

    m=xmail('smtp.163.com','***@163.com','***@163.com','***')
    res= m.send(['***@qq.com'],'測試python發送郵件',cont)
    if res:
        print('sucess')
    else:
        print('fail')      
Python 發送郵件
Python 發送郵件

作者:酷小孩

出處:http://www.cnblogs.com/babycool/

本文首發部落格園,版權歸作者跟部落格園共有。

轉載必須保留本段聲明,并在頁面顯著位置給出本文連結,否則保留追究法律責任的權利。