本文主要是介绍使用python发送QQ邮箱带附件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
from email.header import Header
import smtplib
from email.mime.text import MIMEText #创建邮件内容的文件内容
from email.mime.multipart import MIMEMultipart # 创建带附件的实例
from email.mime.application import MIMEApplication
# 发送带附件邮箱
class Mail():
def __init__(self) -> None:
# 配置第三方smtp服务相关信息
self.mail_host = "SMTP.qq.com"
self.mail_pass = "xxxx" #授权码
self.sender ="xxx@qq.com"
self.receivers = "xxx@qq.com"
# 创建带附件实例
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()
这篇关于使用python发送QQ邮箱带附件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!