发送邮件——stamplib
配置文email.ini件信息:
[email]
sender=xxxxxxxxxxx
pwd=xxxxxxxxxxxx
reciver=xxxxxxxxxxxxx
python 3.x代码如下:
import os,configparser,time,requests,hashlib,json
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def filePath(path):
return os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))),path)
def getEmailData():
#获取配置文件email信息
cf=configparser.ConfigParser()
ConfigPath=filePath('config\\email.ini')
cf.read(ConfigPath)
form_addr=cf.get('email','sender')
pwd=cf.get('email','pwd')
to_addr=cf.get('email','reciver')
return form_addr,pwd,to_addr
def sendEmail(report_path,report_name):
#发送邮件
'''
report_path:发送的文件路径
report_name:发送的文件命名
'''
from_add,pwd,to_user=getEmailData()
fp=open(report_path,'rb')
mail_body=fp.read()
fp.close()
msg=MIMEMultipart()
smtp_server='smtp.exmail.qq.com'
msg['From']=Header(from_add)
msg['To']=Header(to_user)
msg['Subject']=Header(u'私家云接口测试报告','utf-8')
msg['date']=time.strftime('%Y%m%d%H%M')
#发送内容
textpart=MIMEText(mail_body,_subtype='html',_charset='utf-8')
msg.attach(textpart)
#以html附件形式发送
htmlpart=MIMEApplication(open(reportPath(),'rb').read())
htmlpart.add_header('Content-Disposition','attachment',filename=report_name)
msg.attach(htmlpart)
#发送邮件
try:
s=smtplib.SMTP(smtp_server,25)
s.login(from_add,pwd)
s.sendmail(from_add,to_user.split(','),msg.as_string())
s.quit()
log.info(u'邮件发送成功!')
except smtplib.SMTPRecipientsRefused as err:
log.error(u'邮件发送失败!原因为:'+err)
except smtplib.SMTPAuthenticationError as err:
log.error(u'邮件发送失败!原因为:'+err)
except smtplib.SMTPException as err:
log.error(u'邮件发送失败!原因为:'+err)
发送邮件——stamplib的更多相关文章
- 利用SQLServer数据库发送邮件
汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 这个应用案例很多,一般都是预警,比如异常连接的时候,或者数据库报错的时候.等等,,, 先 ...
- C# 多种方式发送邮件(附帮助类)
因项目业务需要,需要做一个发送邮件功能,查了下资料,整了整,汇总如下,亲测可用- QQ邮箱发送邮件 #region 发送邮箱 try { MailMessage mail = new MailMess ...
- JavaMail发送邮件
发送邮件包含的内容有: from字段 --用于指明发件人 to字段 --用于指明收件人 subject字段 --用于说明邮件主题 cc字段 -- 抄送,将邮件发送给收件人的同时抄 ...
- 技术笔记:Indy控件发送邮件
工作中有个需求需要发送邮件,因为使用的delphi6,所以自然就选择了indy组件,想想这事挺简单的.实现的过程倒是简单,看着Indy的demo很快就完了,毕竟也不是很复杂的功能. 功能要求: 1.压 ...
- mono中发送邮件并保存本次收件人的地址
在ios端mono开发中,发送邮件可以选择调用ios原生email程序.有两种方式实现这种功能,一是程序跳转到ipad中email程序,另外一种是将发送邮件的界面在自己应用里弹出. 首先第一种方式的代 ...
- Azure 上通过 SendGrid 发送邮件
SendGrid 是什么? SendGrid 是架构在云端的电子邮件服务,它能提供基于事务的可靠的电子邮件传递. 并且具有可扩充性和实时分析的能力.常见的用例有: 自动回复用户的邮件 定期发送信息给用 ...
- 使用nodemailer发送邮件
今天闲来无事,一时兴起看了下如果使用javascript来发送邮件.经过调研发现,nodeJs可以实现这个功能. 具体的步骤如下: 1.安装依赖 npm install nodemailer -g ( ...
- ASP.NET MVC 发送邮件(异步)
最近写邮件发送搞死人了,最后的结果,真是醉了,现整理如下: 网上一搜一大把,到处都是.NET发送邮件的方法,我这里也大同小异的写了一个. 准备一个MailHelper.cs通用类,如下所示: 重要的命 ...
- MVC5发送邮件注册
#region 发送邮件 //填写电子邮件地址,和显示名称 System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(&qu ...
随机推荐
- c# 实现遍历 DataTable 和DataSet (简单的方式)
今天 做一个小程序 ,遇到了这样一个 问题就是 怎样简单的 遍历一个 DataTable 一. DataTable table= DBhelper.GetDataTable(str);foreach( ...
- 【BZOJ】1857: [Scoi2010]传送带(三分)
http://www.lydsy.com/JudgeOnline/problem.php?id=1857 好神奇的三分.. 第一次写三分啊sad..看了题解啊题解QAQ 首先发现无论怎么走一定是在AB ...
- 开启GitHub模式,now!
(原文地址为:http://www.karottc.com/blog/2014/06/15/current-doing/) 最近看到了一篇文章,该文章的作者将自己连续177天在github上commi ...
- Docker入门与应用系列(六)Docker私有与公共镜像仓库
1.搭建私有镜像仓库 Docker Hub作为Docker默认官方公共镜像:如果想搭建自己的私有镜像仓库,官方提供registry镜像,使搭建私有仓库非常简单 1.1下载registry镜像并启动 d ...
- Java知识点梳理——抽象类和接口
抽象类 1.定义:没有包含足够的信息来描绘一个具体对象的类,不能被实例化,必须被继承: 2.abstract关键字:abstract class定义抽象类,普通类的其它功能依然存在,如变量.方法等: ...
- spring 项目升级到spring cloud记录 数据源配置
用的阿里的数据源 增加pom <dependency> <groupId>com.alibaba</groupId> <artifactId>drui ...
- Pycharm 2017 12月最新激活码
激活的办法:这个必须的联网才可以使用(每次打开PyCharm都需要电脑联网才可以正常使用),要是没网的话,就不能激活使用啦,大家注意哈. http://idea.iteblog.com/key.php ...
- 【BZOJ3123】[Sdoi2013]森林 主席树+倍增LCA+启发式合并
[BZOJ3123][Sdoi2013]森林 Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整 ...
- Blue Bird
Blue Bird 哈巴他一 他拉 毛套拉那 一套一太(他)卖咋西他 闹哇 啊哦一 啊哦一 啊闹扫啦 卡那西米哇马达 哦包爱 啦来字赛次那撒哇姨妈 次卡米哈几卖他阿娜塔爱套一大 靠闹看叫毛姨妈靠逃吧你 ...
- Xcode删除Project上层group
本来想在Project下New Group,结果点了New group from selection, 结果在Project上级新建了一个group,邮件菜单中无删除项…… 解决方法: 1,关闭Xco ...