# 创建带附件实例 def send(self): message = MIMEMultipart() # 创建参数 message['From'] = Header("小黄",'utf-8') message['To'] = Header("小付",'utf-8') message['Subject'] = Header("接口自动化测试", 'utf-8')
# 加入邮件正文内容 message.attach(MIMEText('接口自动化测试报告', 'plain', 'utf-8'))
#构建邮件附件 att = MIMEText(open("D:\\interfaceTest\\result\\test.xlsx","rb").read(),'base64', 'utf-8') #打开附件 att["Content-Type"] = 'application/octet-stream' #设置类别信息 att["Content-Disposition"] = 'attachment; filename="test.xlsx"' # 添加描述信息 message.attach(att) # 加入到邮件中 try: smtpObj = smtplib.SMTP_SSL(self.mail_host, 465) smtpObj.login(self.sender,self.mail_pass) smtpObj.sendmail(self.sender, self.receivers, message.as_string()) smtpObj.quit() print('邮件发送成功') except smtplib.SMTPException as e: print('邮件发送失败')
if __name__ == '__main__': mail= Mail() mail.send()