发送测试报告邮件
- 邮件发送的基本过程与概念
- 1、邮件服务器:类似于现实生活中的邮局,他主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中
- 电子邮箱:用户在邮件服务器上申请的一个账户
- from:<xxx@xx.com> ---发件人
- to:<xxx@xx.com> ---收件人
- subject:hello ---主题
- body: 这是邮件内容 ---内容体
- 2、邮件传输协议
- SMTP协议:全称为simple mail transfer protocol,简称邮件传输协议。它定义了邮件客户端软件和SMTP邮件服务器之间,以及两台SMTP邮件服务器之间的通信规则
- POP3协议:全称post office protocol,邮局协议。它定义了邮件客户端软件和POP3邮件服务器的通信规则
- IMAP协议:全称为Internet message access protocol,Internet消息访问协议,它是对POP3协议的一种扩展
"""此处示例发送方为QQ邮箱,接收方为腾讯企业邮箱"""
import smtplib
import time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class MailUtils():
@classmethod
def send_test_mail(cls):
sender = "xxxx@foxmail.com"
receiver = ["xxxx@enable-ets.com","xxxx@163.com"]
auth_code = "xxxx"
subject = "这是一条测试邮件"
f = open("D:\Study\TestCase\\TestCast_result.html", "rb")
mail_body = f.read()
f.close()
html = MIMEText(mail_body, _subtype="html", _charset="utf-8")
html["Subject"] = subject
html["from"] = sender
html["to"] = ';'.join(receiver)
email_file = MIMEText(mail_body, 'base64', 'gb2312')
email_file["Content-Type"] = 'application/octet-stream'
email_file["Content-Disposition"] = 'attachment; filename="TestCase.html"'
email_liction = MIMEMultipart()
email_liction['Subject'] = subject
email_liction['From'] = sender
email_liction['To'] = ';'.join(receiver)
email_liction.attach(html)
email_liction.attach(email_file)
smtp = smtplib.SMTP()
smtp.connect("smtp.qq.com")
smtp.login(sender, auth_code)
smtp.sendmail(sender, receiver, email_liction.as_string())
smtp.quit()
if __name__ == '__main__':
MailUtils.send_test_mail()