首页 > 代码库 > python中关于发邮件的示例
python中关于发邮件的示例
发送邮件示例代码如下:
from WebUtils import ProperitiesLoad from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import smtplib import os class Sendmails(): ‘‘‘ 发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件, 如果有多个附件,可依次构造,最后利用smtplib.smtp发送。 ‘‘‘ def __init__(self): mail=ProperitiesLoad.LodingProperities("../resources/Config.ini") self.sentfrom=mail.loading("mail","sentfrom") self.username=mail.loading("mail","username") self.password=mail.loading("mail","password") self.sentto=mail.loading("mail","sentto") self.smtphost=mail.loading("mail","smtphost") self.contect=mail.loading("mail","content") def send(self,attachementfilepath): #创建一个带附件的实例 self.msg = MIMEMultipart() attachement=MIMEText(open(attachementfilepath,‘rb‘).read(),‘base64‘, ‘gb2312‘) self.msg.add_header(‘Content-Disposition‘, ‘attachment‘, filename=os.path.basename(attachementfilepath)) #加邮件头 self.msg[‘to‘] = self.sentto self.msg[‘from‘] = self.sentfrom self.msg[‘subject‘] = self.contect self.msg #发送邮件 try: server = smtplib.SMTP() server.connect(self.smtphost) server.login(self.username,self.password) server.sendmail(self.msg[‘from‘],self.msg[‘to‘],self.msg.as_string()) server.quit() print ‘发送成功‘ except Exception, e: print str(e)
python中关于发邮件的示例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。