python自动化测试怎么发送邮件_python自动发送测试报告,163邮箱测试通过

#注明:from博客小五义http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html

# -*- coding: utf-8 -*-

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

import smtplib

import os,datetime,time

mailto_list=["gutongjie@bianfeng.com"]

mail_host="smtp.bianfeng.com"  #设置服务器

mail_user="gutongjie"  #用户名

mail_pass="meiyoumima123!@#"  #口令

mail_postfix="bianfeng.com"  #发件箱的后缀

#创建一个带附件的实例

msg = MIMEMultipart()

#构造附件1

result_dir = "D:\Program

Files\\Python\\zjxw\\unittest005\\log"

#print result_dir

lists = os.listdir(result_dir)

#print lists

lists.sort(key=lambda fn: os.path.getmtime(result_dir+"\\"+fn)

if not

#lists.sort()Python 列表有一个内置的列表。sort()方法用于改变列表中元素的位置。还有一个

sorted()内置函数,

#key=lambda fn:  用fn来进行排序 fn是斐波那契函数

#os.path.getmtime()

返回文件列表中最新文件的时间(最新文件的时间最大,所以我们会得到一个最大时间)

os.path.isdir(result_dir+"\\"+fn) else 0)

#os.path.isdir() 函数判断某一路径是否为目录。

#print lists[-1]

#打印最新的文件列表

#lists[-1]  表示取文件列表中的最大值,也就是最新被创建的文件。

file = os.path.join(result_dir,lists[-1])

#print file

#os.path.join()

方法用来连接字符串,通过路径与文件名的拼接,我们将得到目录下最新被创建的的文件名的完整路径。

att1 = MIMEText(open(file, 'rb').read(), 'base64',

'utf-8')

print att1

att1["Content-Type"] = 'application/octet-stream'

att1["Content-Disposition"] = 'attachment;

filename="TestReport.html"'#这里的filename可以任意写,写什么名字,邮件中显示什么名字

msg.attach(att1)

#构造附件2

#att2 = MIMEText(open('d:\\123.txt', 'rb').read(), 'base64',

'gb2312')

#att2["Content-Type"] = 'application/octet-stream'

#att2["Content-Disposition"] = 'attachment;

filename="123.txt"'

#msg.attach(att2)

#加邮件头

msg['to'] = 'gutongjie@bianfeng.com'

msg['from'] = 'chuckgu002@163.com'

msg['subject'] = 'hello world'

#发送邮件

try:

server =

smtplib.SMTP()

server.connect('smtp.163.com')

server.login('chuckgu002','123456')#XXX为用户名,XXXXX为密码

server.sendmail(msg['from'], msg['to'],msg.as_string())

server.quit()

print '发送成功'

except Exception, e:

print str(e)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值