py使用邮件发送测试报告

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# -----------1、发件相关的参数---------------
smtpserver = "smtp.163.com" # 发件服务器
port = 0 # 端口
sender = "m18581893850@163.com" # 发件人
psw = "xxxxxxxxxxxxxx" # 密码
receiver = "635889832@qq.com" # 收件人
# receiver = ["639889836@qq.com", "635889845@qq.com"] # 群发用列表

# -----------2、编辑邮件内容---------------
# -----------2、1只发送邮件---------------


subject1 = "给XXX的一封信"
body = '<p>我们约会吧</p>' # 邮件正文用html格式
msg = MIMEText(body, "html", "utf-8")
msg['from'] = sender
msg['to'] = receiver
msg['subject'] = subject1
# -----------2、2发送附件-------------------
# 读文件
subject2 = "给帅哥的一张图片"
file_path = "E:\\test_demo\\report\\report.html"
with open(file_path, "rb") as fp:
mail_body = fp.read()
msg = MIMEMultipart()
msg["from"] = sender # 发件人
msg["to"] = receiver # 收件人
# msg["to"] = ";".join(receiver) # 群发收件人
msg["subject"] = subject2 # 主题
# 正文
body = MIMEText(mail_body, "html", "utf-8")
msg.attach(body)
# 附件
att = MIMEText(mail_body, "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename="test_report.html"'
msg.attach(att)

# ----------3、发送邮件---------------------
try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver) # 连服务器
smtp.login(sender, psw)
except:
smtp = smtplib.SMTP_SSL(smtpserver, port)
smtp.login(sender, psw) # 登录
smtp.sendmail(sender, receiver, msg.as_string()) # 发送
smtp.quit() # 关闭

 

------能发送成功,频繁使用会企业退信

 

转载于:https://www.cnblogs.com/wapn/p/9613551.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值