语言:python

参考:https://www.runoob.com/python/python-email.html

前提:

1、QQ邮箱开启了SMTP服务

 

 2、生成了授权码,这个授权码将作为自己的邮箱密码

 

 

代码:(如果没有相关包需要自己引入)

import  xlrd
import time
import  xlsxwriter
from xlutils.copy import copy
from email.mime.multipart import MIMEMultipart
from email.header import Header
import smtplib
from email.mime.text import MIMEText

host = 'smtp.qq.com' #qq邮箱服务器地址
port = 465 #qq邮箱默认端口号
user = "xxxxxx@qq.com"  #发件人邮箱号
password = "kofffdqqdiji" #发件人授权码
sender = "xxxxxx@qq.com" #发件人邮箱号
receivers = ['xxxxxx@qq.com']  # 创建收件人账号列表,可以和发件人邮箱一样
subject = 'Python邮件测试'# 邮件标题
# MIMEText有三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
#message = MIMEText('Python 邮件发送测试', 'plain', 'utf-8')
try:
    message = MIMEText('Python 邮件发送测试', 'plain', 'utf-8')
    message['Subject'] = Header(subject, 'utf-8')
    #%%
    message['From'] = 'xxxxxx@qq.com '#发件人邮箱
    #%%
    message['To'] = ';'.join(receivers)#引入收件人邮箱列表

    smtp_obj = smtplib.SMTP_SSL(host)

    # 开启发信服务,加密传输。python3.7一定要填括号里边的“host”否则会保持会报错: #ValueError: server_hostname cannot be an empty string or start with a leading dot.

    smtp_obj.connect(host, port)
    smtp_obj.login(user, password) # 登录邮箱
    smtp_obj.sendmail(sender, receivers, message.as_string()) #发送邮件
    print ("邮件发送成功")
except smtplib.SMTPException:
    print ("邮件发送失败")

 

后序:可以结合自动化测试来实现自动发送测试报告

参考链接:https://blog.csdn.net/qq_39954916/article/details/105901966

版权声明:本文为XiqueBlogs原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/XiqueBlogs/p/14607720.html