python发送邮件及Jenkins持续集成

使用xxx@163.COM邮箱,获取授权密码:XLQCNKUHWHIXJQ     ,不同的邮箱生成的不一样

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")

一、开通SMTP和POP3协议

二、编写发送邮件类

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/6 17:50
# @File    : email_manage.py
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


class EmailManage:
    
    def send_email(self, report_name):
        # 定义SMTP服务器
        smtpserver = 'smtp.163.com'
        # 发送邮件的用户名和客户端密码
        username = 'lxm@bonc.com.cn'
        password = 'Knl'#授权密码

        # 接收邮件的邮箱
        receiver = '269108973@qq.com,265452325@22.com'

        # 创建邮件对象
        message = MIMEMultipart('related')
        subject = '邮件的主题目'  #邮件的主题
        fujian = MIMEText(open(report_name, 'rb').read(), 'html', 'utf-8')# 附件
        # 把邮件的信息组装到邮件对象里面
        message['form'] = username
        message['to'] = receiver
        message['subject'] = subject
        message.attach(fujian)
        # 登录smtp服务器并发送邮件
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver)
        smtp.login(username, password)
        smtp.sendmail(username, receiver, message.as_string())
        smtp.quit()

生成html文件,并发送邮件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/7 11:18
# @File    : test_ecshop.py
import time
import unittest
import HTMLTestRunner

from email_test.email_manage import EmailManage


class TestInfo(unittest.TestCase):

    def test_01(self):
        print("测试01")

    def test_02(self):
        print("测试02")


if __name__ == '__main__':
    suite = unittest.defaultTestLoader.discover('./', '*.py')
    files = open('./report.html', 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(stream=files, title='测试报告', description='报告描述')
    runner.run(suite)
    files.close()# 在发送之前一定要将文件流关闭掉
    time.sleep(3)

    EmailManage().send_email(files.name)

三、使用jenkins,需要安装EmailExtention

 

构建触发器

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/7 11:18
# @File    : test_ecshop.py
import time
import unittest
import HTMLTestRunner

from email_test.email_manage import EmailManage

if __name__ == '__main__':
    suite = unittest.defaultTestLoader.discover('./', '*.py')
    files = open('./report.html', 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(stream=files, title='测试报告', description='报告描述')
    runner.run(suite)
    files.close()# 在发送之前一定要将文件流关闭掉
    time.sleep(3)

下边的【系统管理员邮件地址】,必须和发送人保持一致

 

 

 

 

 

 

 

 

 

 

 

 

 

 


版权声明:本文为lixiaomei0623原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。