欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python3通过qq邮箱发送邮件以及附件

时间:2021-03-14 09:50:12|栏目:Python代码|点击:

本文实例为大家分享了python3通过qq邮箱发送邮件以及附件的具体代码,供大家参考,具体内容如下

开启qq邮箱的smtp服务

代码:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


def Mailer(to_list,th1=None,Subject=None,unipath=None):

 mail_host = 'smtp.qq.com'  # 邮箱服务器
 mail_user = 'dalu@qq.com' # 发件人邮箱密码(当时申请smtp给的口令)
 mail_pwd = '***********' # SMTP密码
 s = smtplib.SMTP_SSL(mail_host, 465,timeout=5)
 s.login(mail_user, mail_pwd)
 #邮件内容
 mail = str(th1)
 msg = MIMEMultipart()
 msgtext = MIMEText(mail.encode('utf8'), _subtype='html', _charset='utf8')
 msg['From'] = mail_user
 msg['Subject'] = Subject
 msg['To'] = ",".join(to_list)

 if unipath is not None:
  att1 = MIMEText(open(unipath, 'rb').read(), 'base64', 'gb2312')
  att1["Content-Type"] = 'application/octet-stream'
  att1.add_header('Content-Disposition', 'attachment',filename=(Subject+ '.xlsx'))
  msg.attach(att1)
 msg.attach(msgtext)
 try:
  s.sendmail(mail_user, to_list, msg.as_string())
  s.close()
  print('发送成功')
 except Exception as e:
  print(e)

to_list = [
 #多用户使用的list
 'dalu@qq.com',
]

Mailer(to_list,th1="这是要发的邮件内容",Subject='邮件标题',unipath=r'F:\test.xlsx')

上一篇:Python 26进制计算实现方法

栏    目:Python代码

下一篇:Python面向对象实现一个对象调用另一个对象操作示例

本文标题:python3通过qq邮箱发送邮件以及附件

本文地址:http://www.codeinn.net/misctech/80511.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有