python使用qq邮箱发送带有附件的邮件

from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
from email.mime.multipart import MIMEMultipart

host_server = 'smtp.qq.com'#使用qq邮箱smtp服务器
sender_qq = 'example'#sender_qq为发件人的qq号
pwd = '****'#pwd为qq邮箱的授权码
sender_qq_mail = 'example@qq.com'  #发件人的邮箱
receiver = 'shouxinren@163.com'  #收件人邮箱
mail_title = '这是标题'  #邮件标题
smtp = SMTP_SSL(host_server)  #ssl登录邮箱客户端
smtp.set_debuglevel(1)#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp.ehlo(host_server)
smtp.login(sender_qq, pwd)
##定义邮箱内容
msg = MIMEMultipart()   ##生成一个邮箱对象
msg["Subject"] = Header(mail_title, 'utf-8')   ##添加邮箱主题
msg["From"] = sender_qq_mail   ##添加邮箱发送方
msg["To"] = receiver   ##添加邮箱接收方
# 添加附件,这里以zip文件为例
file_path=r'test.zip'
att = MIMEText(open(file_path,"rb").read(), "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment;filename = {}'.format(file_path)
msg.attach(att)
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值