zoukankan      html  css  js  c++  java
  • python 测试报告发送邮件

    使用过程成出现的如下错误

    smtplib.SMTPDataError: (554, 'DT:SPM 126 smtp5错误解决办法

     

    1.自动化测试中,调用邮件模块自动发送邮件时,运行脚本报错:

    smtplib.SMTPDataError: (554, 'DT:SPM 126 smtp5,jtKowAD3MJz2c1JXLcK2AA--.52114S2 1465021431,please see http://mail.163.com/help/help_spam_16.htm?ip=123.114.121.110&hostid=smtp5&time=1465021431')

    2.解决方法(这里已python为例):

    #定义发送邮件
    def send_mail(file_new):
    f = open(file_new, 'rb')
    mail_body = f.read()
    f.close()

    msg = MIMEText(mail_body, 'html', 'utf-8')
    msg = MIMEText('请查看附件内容!','plain','utf-8')
    msg['Subject'] = Header("自动化测试报告", 'utf-8')

    #报错原因是因为“发件人和收件人参数没有进行定义
    msg['from'] = 'test_bug@126.com'
    msg['to'] = 'testyao@163.com'

    smtp = smtplib.SMTP()
    smtp.connect("smtp.126.com")
    smtp.login("test_bug@126.com", "登录密码")
    smtp.sendmail("test_bug@126.com","testyao@163.com", msg.as_string())
    smtp.quit()
    print('邮件发送成功email has send out !')

     3.利用此种方法(绿色代码部分)即可解决相关邮箱的554, 'DT:SPM的错误。

    本文为"测试那点事_TestYao"博客整理收集,转载请注明出处,谢谢!

     以下代码是通过python3实现了 邮件的发送

    # coding:utf-8
    
    import os, sys
    import smtplib
    import time
    from email.header import Header
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    
    reportPath = os.path.join(os.getcwd(), 'report')  # 测试报告的路径
    
    print("打印路径:")
    
    print(reportPath)
    
    
    # reportPath = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])),'report')
    
    class SendMail(object):
        def __init__(self, recver=None):
            """接收邮件的人:list or tuple"""
            if recver is None:
                self.sendTo = ['chinayyj2010@163.com']  # 收件人这个参数,可以是list,或者tulp,以便发送给多人
            else:
                self.sendTo = recver
    
        def get_report(self):  # 该函数的作用是为了在测试报告的路径下找到最新的测试报告
            dirs = os.listdir(reportPath)
            dirs.sort()
            newreportname = dirs[-1]
            print('The new report name: {0}'.format(newreportname))
            return newreportname  # 返回的是测试报告的名字
    
        def take_messages(self):  # 该函数的目的是为了 准备发送邮件的的消息内容
            newreport = self.get_report()
            self.msg = MIMEMultipart()
            self.msg['Subject'] = '测试报告主题'  # 邮件的标题
            self.msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
    
            with open(os.path.join(reportPath, newreport), 'rb') as f:
                mailbody = f.read()  # 读取测试报告的内容
            html = MIMEText(mailbody, _subtype='html', _charset='utf-8')  # 将测试报告的内容放在 邮件的正文当中
            self.msg.attach(html)  # 将html附加在msg里
    
            # html附件    下面是将测试报告放在附件中发送
            att1 = MIMEText(mailbody, 'base64', 'gb2312')
            att1["Content-Type"] = 'application/octet-stream'
            att1["Content-Disposition"] = 'attachment; filename="TestReport.html"'  # 这里的filename可以任意写,写什么名字,附件的名字就是什么
            self.msg.attach(att1)
    
        def send(self):
            self.take_messages()
            self.msg['from'] = 'wmXXXXXt@163.com'  # 发送邮件的人
            self.msg['to'] = 'cXXXXXXX@163.com'     # 收件人和发送人必须这里定义一下,执行才不会报错。
            #smtp = smtplib.SMTP('smtp.163.com', 25)  # 连接服务器
            smtp = smtplib.SMTP()
            smtp.connect('smtp.163.com')
            smtp.login('wmqyyj_test@163.com', 'XXXXXXX')  # 登录的用户名和密码(注意密码是设置客户端授权码,因为使用用户密码不稳听,有时无法认证成功,导致登录不上,故无法发送邮件。)
            smtp.sendmail(self.msg['from'], self.msg['to'], self.msg.as_string())  # 发送邮件
            smtp.close()
            print('sendmail success')
    
    
    if __name__ == '__main__':
        sendMail = SendMail()
        sendMail.send()
  • 相关阅读:
    第一次MVC记录
    Treeview绑定以及添加多选功能
    BindingSource的使用
    WPF实现环(圆)形进度条
    WPF实现环(圆)形菜单
    WPF实现音乐字幕动画
    WPF加载高德地图
    WPF实现Android(3D)菜单翻转动画
    WPF实现头像裁剪
    WPF PointAnimationUsingKeyFrames 动画
  • 原文地址:https://www.cnblogs.com/Skyyj/p/6670745.html
Copyright © 2011-2022 走看看