python中把生成测试报告html文件发送到邮箱报'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte错
import smtplib#发送邮件
import os
#封装邮件内容
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
class mailsend():
def sendFujian(self,filename):
# 如果是qq邮箱,就改为"smtp.qq.com"
smtpserver = "smtp.163.com"
#设置邮箱的用户名及密码,用于邮箱登录
username = "zhouz199@163.com"
# 不是使用登录密码,授权码
password = ""
#设置发送邮箱(发件人)
sender = "zhouz199@163.com"
#设置接收邮箱(收件人)
receiver = "118752@qq.com"
#设置邮箱主题
subject = "自动化测试结果"
# 获取当前文件所在的全路径的绝对值
# 这两句代码是重点关注的,你要指定去哪个目录下去找你的html报告文件
path2 = os.path.abspath((os.path.dirname(__file__)))+r"/"
# path指的是html文件的全路径
path = path2+filename+r".html"
content = open(path,"rb").read()
msg = MIMEText(content,"base64","utf-8")
msg["Content-Type"]="application/octet-stream"
msg['Content-Disposition'] = "attachment;filename='%s.html'" %filename
msgRoot = MIMEMultipart('related')
msgRoot["Subject"]=subject
msgRoot["From"] = sender
msgRoot["To"]= receiver
msgRoot.attach(msg)
#创建一个邮件发送服务的对象
smtp = smtplib.SMTP()
#连接发件服务器
smtp.connect(smtpserver)
#登录发件邮箱
smtp.login(username,password)
#发送邮件
smtp.sendmail(sender,receiver,msgRoot.as_string())
smtp.quit()
if __name__=="__main__":
ssend = mailsend()
ssend.sendFujian(r"2022-05-26-15-17-25")
报错图片!!
各种方法都尝试过了就是不行,在别人的电脑上能运行,就在我的电脑上不行,是为什么啊,求指导
把utf-8改成gbk试试