标签:sendmail receive stream smtplib tac 邮件发送 邮箱服务器 filename code
标签(空格分隔): 带附件邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart # 用于传送附件
smtpserver = 'smtp.exmail.qq.com'
user = '***@zhan.com'
#这里是邮箱的授权码
password = '****'
#这里是发送者,收件者的邮箱的定义
sender = '****@zhan.com'
receiver = '***@qq.com'
# 发送邮件的标题和内容
subject = 'web selenium 自动化测试报告'
content = '<html><h1 style = "color:red">测试报告2017</h1></html>'
# 构造附件内容
send_file = open(r"H:\l.png", 'rb').read()
att = MIMEText(send_file, 'base64', 'utf-8')
att['Content-Type'] = 'application/octet-stream'
att['Content-Disposition'] = 'attachment;filename="l.png"'
# 构建发送与接收信息
msgRoot = MIMEMultipart()
msgRoot.attach(MIMEText(content, 'html', 'utf-8'))
msgRoot['subject'] = subject
#这里填写发送邮箱,和收件邮箱
msgRoot['from'] = '****@zhan.com'
msgRoot['To'] = '****@qq.com'
msgRoot.attach(att)
# ssl 协议端口号要使用465
smtp = smtplib.SMTP_SSL(smtpserver, 465)
# 向用户表示用户的身份
smtp.helo(smtpserver)
# 服务器返回结果确认
smtp.ehlo(smtpserver)
# 登录邮箱服务器用户名和密码
smtp.login(user, password)
print("发送邮件")
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit
print("邮件发送完成")
标签:sendmail receive stream smtplib tac 邮件发送 邮箱服务器 filename code
原文地址:https://www.cnblogs.com/surewing/p/9385005.html