python实现smtp发送邮件类-直接调用就好

来源:互联网 发布:西方建筑欣赏知乎 编辑:程序博客网 时间:2024/06/06 16:49

python实现smtp发送邮件类

使用方法

#使用方法Sendmail('stmp服务器', '邮箱', '密码', 发送的邮箱用list格式,可多个发送)p = Sendmail('smtp.minshengec.cn', 'wangqi@minshengec.cn', 'xxoofor you', ['wangqi@minshengec.cn'])#setMailInfo('标题', '内容')p.setMailInfo('dsadsa', 'xxoo')#最后运行run方法p.run()
#!/usr/bin/env python# -*- coding: gbk -*-#导入smtplib和MIMETextimport smtplibfrom email.mime.text import MIMETextclass Sendmail(object):    def __init__(self, mailHost, mailUser, mailPwd):        self.mailHost = mailHost;        self.mailUser = mailUser;        self.mailPwd = mailPwd;        self.sendList = [];#function subject->title    def setMailInfo(self, sendList,subject, content):        self.sendList = sendList        self.content = content;        self.subject = subject;        self.msg = MIMEText(self.content)        self.msg['From'] = self.mailUser        self.msg['Subject'] = self.subject        self.msg['To'] = ";".join(self.sendList)    def run(self):        try:            self.send = smtplib.SMTP()            self.send.connect(self.mailHost)            self.send.login(self.mailUser, self.mailPwd)            self.send.sendmail(self.mailUser, self.sendList, self.msg.as_string())            print '[*]-----send mail---to' + str(self.sendList) + 'success-----[*]'        except smtplib.SMTPException as e:            print e#p = Sendmail('smtp.minshengec.cn', 'wangqi@minshengec.cn', 'xxoo for you')#p.setMailInfo(['wangqi@minshengec.cn'], 'dsadsaqqq', 'xxodsado')#p.run()

这里写图片描述

0 0