配置文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的更多相关文章

  1. 利用SQLServer数据库发送邮件

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 这个应用案例很多,一般都是预警,比如异常连接的时候,或者数据库报错的时候.等等,,, 先 ...

  2. C# 多种方式发送邮件(附帮助类)

    因项目业务需要,需要做一个发送邮件功能,查了下资料,整了整,汇总如下,亲测可用- QQ邮箱发送邮件 #region 发送邮箱 try { MailMessage mail = new MailMess ...

  3. JavaMail发送邮件

    发送邮件包含的内容有: from字段  --用于指明发件人 to字段      --用于指明收件人 subject字段  --用于说明邮件主题 cc字段     -- 抄送,将邮件发送给收件人的同时抄 ...

  4. 技术笔记:Indy控件发送邮件

    工作中有个需求需要发送邮件,因为使用的delphi6,所以自然就选择了indy组件,想想这事挺简单的.实现的过程倒是简单,看着Indy的demo很快就完了,毕竟也不是很复杂的功能. 功能要求: 1.压 ...

  5. mono中发送邮件并保存本次收件人的地址

    在ios端mono开发中,发送邮件可以选择调用ios原生email程序.有两种方式实现这种功能,一是程序跳转到ipad中email程序,另外一种是将发送邮件的界面在自己应用里弹出. 首先第一种方式的代 ...

  6. Azure 上通过 SendGrid 发送邮件

    SendGrid 是什么? SendGrid 是架构在云端的电子邮件服务,它能提供基于事务的可靠的电子邮件传递. 并且具有可扩充性和实时分析的能力.常见的用例有: 自动回复用户的邮件 定期发送信息给用 ...

  7. 使用nodemailer发送邮件

    今天闲来无事,一时兴起看了下如果使用javascript来发送邮件.经过调研发现,nodeJs可以实现这个功能. 具体的步骤如下: 1.安装依赖 npm install nodemailer -g ( ...

  8. ASP.NET MVC 发送邮件(异步)

    最近写邮件发送搞死人了,最后的结果,真是醉了,现整理如下: 网上一搜一大把,到处都是.NET发送邮件的方法,我这里也大同小异的写了一个. 准备一个MailHelper.cs通用类,如下所示: 重要的命 ...

  9. MVC5发送邮件注册

    #region 发送邮件 //填写电子邮件地址,和显示名称 System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(&qu ...

随机推荐

  1. 字符串的数字部分递增,如user00000001后面的数字部分递增+1

    使用存储过程方式 bengin declare@namevarchar(50) set@name=(selectmax(right('user00000001',8<8是从后往前数,从1开始&g ...

  2. Linux基础命令(2)

      Fskey servername scp命令 grep 命令 find 命令 echo 命令 xargs 命令 file 命令 cat 命令 /dev/null tar 打包 gzip 压缩 示例 ...

  3. 经典Mathematica函数大全

    转自:http://blog.renren.com/share/238323208/8426343822  Mathmatic 函数表  一.运算符及特殊符号 Line1; 执行Line,不显示结果  ...

  4. 在项目中用过Spring的哪些方面?及用过哪些Ajax框架?

    在项目中用过Spring的哪些方面?及用过哪些Ajax框架? 解答:在项目使用过Spring IOC ,AOP,DAO,ORM,还有上下文环境. 在项目使用过Ext,Juery等Ajax框架.

  5. ReactJS组件之间通信

    http://www.open-open.com/lib/view/open1473838243065.html

  6. 俄罗斯方块——shell

    #!/bin/bash # Tetris Game # xhchen<[email]xhchen@winbond.com.tw[/email]> #APP declaration APP_ ...

  7. [原创]adb使用教程v1.1.0-----by-----使用logcat快速抓取android崩溃日志

    原文再续,书接上回:<使用logcat快速抓取android崩溃日志>中提到的工具包可以下载拉~ <使用logcat快速抓取android崩溃日志>:http://www.cn ...

  8. CentOS下搭建LNMP+WordPress+http2.0教程

    此文是本人CentOS下搭建WordPress的一些笔记,环境搭建时间::将看过的几篇文章总结下来,形成一条龙长文.不用大家再找来找去. 本文大概分为此几部分: 一.基础命令更新: 二.服务器加速(非 ...

  9. spring boot rabbitmq 多MQ配置 自动 创建 队列 RPC

      源码地址:https://github.com/hutuchong518/RabbitmqStudy 需求:   spring boot 整合 rabbitmq rpc功能, 需要将 请求和响应 ...

  10. ssh框架搭建出现的问题和解决

    [说明]今天尝试从头开始搭建ssh 框架, 真心是有点不太容易,可能是第一次吧,之前都是小打小闹. 一:今日完成 搭建 Spring 环境 --> 然后搭建 Hibernate 环境 --> ...