使用python发送QQ邮箱带附件
2022/1/7 14:03:40
本文主要是介绍使用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邮箱带附件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-03用FastAPI掌握Python异步IO:轻松实现高并发网络请求处理
- 2025-01-02封装学习:Python面向对象编程基础教程
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型