您的位置:首页 > 编程语言 > Python开发

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

2015-09-22 16:23 537 查看

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和MIMEText

import smtplib
from email.mime.text import MIMEText

class 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()


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: