python实现邮件循环自动发件功能

作者:liyali2020 时间:2021-01-20 02:39:24 

发邮件是一种很常见的操作,本篇主要介绍一下如何用python实现自动发件。


import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.image import MIMEImage
import time
mail_host="smtp.126.com"
mail_user="xxx@126.com"
mail_pass="******"#注意如果邮箱开启了授权码,此处要填写授权码,否则会报smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed')
sender="xxx@126.com"
receiver = ['邮箱1','邮箱2']#群发邮件
for i in range(n):#自定义循环发多少遍
try:
message = MIMEMultipart()
message["From"] = Header(sender)
message["To"] = ','.join(receiver)
message["Subject"] = Header("主题", "utf-8").encode()#主题
message.attach(MIMEText("正文", "plain", "utf-8"))#正文
"""
定附件
"""
att = MIMEText(open(r'C:\Users\Administrator\Desktop\1.txt').read(), "base64", "utf-8")
att["Content-Type"] = 'application/octet-stream'
 att.add_header("Content-Disposition", 'attachment', filename="1.txt")#这一步可避免文件不能正常打开
message.attach(att)
"""
构造图片(以附件形式上传)
"""
image = MIMEImage(open(r'C:\Users\Administrator\Desktop\1.jpg', 'rb').read())
image.add_header('Content-ID', '<image1>')#可避免图片不能正常打开
image["Content-Disposition"] = 'attachment; filename="picture.jpg"'
message.attach(image)
"""
发送邮件
"""
smtp = smtplib.SMTP_SSL(host=mail_host)
smtp.connect(host=mail_host, port=465)
smtp.login(mail_user, mail_pass)
smtp.sendmail(sender, message['To'].split(','), message.as_string())
print("在%s第" % ctime(), str(i+1), "封邮件发送")
smtp.quit()
except smtplib.SMTPException as e:
 raise e

最终实现

python实现邮件循环自动发件功能

来源:https://blog.csdn.net/liyali2020/article/details/108463287

标签:python,发邮件,自动发件
0
投稿

猜你喜欢

  • 从HTTP状态 301,302,200 来看页面跳转

    2007-09-26 13:46:00
  • python实现视频压缩功能

    2023-12-14 02:48:36
  • Python中扩展包的安装方法详解

    2021-09-19 23:35:46
  • Python提取支付宝和微信支付二维码的示例代码

    2021-04-08 19:38:42
  • python 面向对象开发及基本特征

    2022-09-03 01:48:43
  • Bootstrap显示与隐藏简单实现代码

    2023-08-24 17:30:54
  • 解决Python中导入自己写的类,被划红线,但不影响执行的问题

    2021-07-11 19:10:29
  • Python切片操作实例分析

    2022-05-02 17:49:25
  • asp中字符编码转换的10个函数[荐]

    2007-11-11 10:32:00
  • PyTorch中的参数类torch.nn.Parameter()详解

    2021-09-07 19:06:30
  • Python统计列表元素出现次数的方法示例

    2021-03-06 00:43:23
  • 一款强大的端到端测试工具Playwright介绍

    2021-06-19 11:59:55
  • sina和265天气预报调用代码

    2007-11-19 13:32:00
  • pycharm激活码免费分享适用最新pycharm2020.2.3永久激活

    2021-12-11 10:17:09
  • ASP实现网页打开任何类型文件都保存的方法

    2007-12-21 13:10:00
  • 取巧的边框等高

    2009-12-16 12:11:00
  • pytorch中交叉熵损失(nn.CrossEntropyLoss())的计算过程详解

    2021-06-03 09:28:09
  • python内置模块collections知识点总结

    2023-10-27 19:37:08
  • Python基础教程(一)——Windows搭建开发Python开发环境

    2021-06-16 13:41:53
  • Python reques接口测试框架实现代码

    2023-10-07 12:47:08
  • asp之家 网络编程 m.aspxhome.com