接口自动化(六)--使用QQ邮箱发送邮件

接口测试执行完发送一个邮件,这里使用QQ邮箱发送,先要拿到QQ邮箱授权码,方法自行百度

 1 # coding=utf-8
 2 import smtplib
 3 from email.mime.text import MIMEText
 4 
 5 class sendEmail():
 6     def __init__(self,sender,password,resever,subject,content):
 7         self.sender = sender
 8         self.password = password           #qq邮箱授权码
 9         self.reserver = resever
10         self.subject = subject
11         self.content = content
12 
13     def send(self):
14         message = MIMEText(self.content)
15         message["subject"] = self.subject
16         message["from"] = self.sender
17         message["to"] = self.reserver
18         try:
19             server = smtplib.SMTP_SSL("smtp.qq.com",465)
20             server.login(self.sender,self.password)
21             server.sendmail(self.sender,self.reserver,message.as_string())
22             print("邮件发送成功")
23         except Exception as e:
24             print("邮件发送失败")
25         finally:
26             server.quit()

 另外封装了一个模块,为了调用简单(直接RunSendMail(passlist,faillist).sendmail()就可以了)

 1 from common import sendemail
 2 
 3 class RunSendMail():
 4     def __init__(self,passlist,faillist):
 5         self.passnum = passlist
 6         self.failnum = faillist
 7         self.sender  = '#######@qq.com'
 8         self.password = '*********'          #QQ邮箱授权码
 9         self.reserver = '#########@qq.com'
10         self.subject = "python邮件测试"
11 
12     def config(self):
13         passnum = len(self.passnum)
14         failnum = len(self.failnum)
15         totalnum = passnum + failnum
16         passrate = "%2.f%%"%(passnum/totalnum*100)
17         failrate = "%2.f%%"%(failnum/totalnum*100)
18         content = "根据需要自己定义邮件内容" 
19         return content
20 
21     def sendmail(self):       
22    sendemail.sendEmail(self.sender,self.password,self.reserver,self.subject,self.config()).send()

 

转载于:https://www.cnblogs.com/kakaln/p/8401805.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值