python3:利用SMTP协议发送QQ邮件+附件

转载请表明出处:https://www.cnblogs.com/shapeL/p/9115887.html

1.发送QQ邮件,首先必须知道QQ邮箱的SMTP服务器

http://service.mail.qq.com/cgi-bin/help?id=28&no=167&subtype=1

2.发送邮件之前,必须开启qq邮箱的smtp服务

设置路径:邮箱设置--账户--开启截图服务--保存更改

3.代码抛出异常分析

(1)邮箱密码传入值为日常登录密码,报错

global send_user
global email_host
global password

password = 'xxx92'
email_host = "smtp.qq.com"
send_user = "11xxx@qq.com"

抛出异常:

smtplib.SMTPAuthenticationError:(535, b'Error: \xc7\xeb\xca\xb9\xd3\xc3\xca\xda\xc8\xa8\xc2\xeb\xb5\xc7\xc2\xbc\xa1\xa3\xcf\xea\xc7\xe9\xc7\xeb\xbf\xb4: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')

打开抛出异常中的链接:是关于授权码的介绍,根据介绍,登录时应该使用授权码作为登录密码,该处的授权码是开启服务时收到的16位授权码

http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

修改代码:

password = "lunkbrgwqxhfjgxx"(对应的16位授权码)

(2)安全邮件,需要通过SSL发送

server = smtplib.SMTP()
server.connect(email_host,25)

抛出异常:

smtplib.SMTPServerDisconnected: Connection unexpectedly closed

QQ邮箱是支持安全邮件的,需要通过SSL发送的邮件:使用标准的25端口连接SMTP服务器时,使用的是明文传输,发送邮件的整个过程可能会被窃听。要更安全地发送邮件,可以加密SMTP会话,实际上就是先创建SSL安全连接,然后再使用SMTP协议发送邮件

修改代码:

server = smtplib.SMTP_SSL()
server.connect(email_host,465)# 启用SSL发信, 端口一般是465

4.附上完整代码

#coding:utf-8
import smtplib
from email.mime.text import MIMEText

class SendEmail:
    global send_user
    global email_host
    global password
    password = "lunkbrgwqxhfjgxx"
    email_host = "smtp.qq.com"
    send_user = "11xx@qq.com"

    def send_mail(self,user_list,sub,content):
        user = "shape" + "<" + send_user + ">"
        message = MIMEText(content,_subtype='plain',_charset='utf-8')
        message['Subject'] = sub
        message['From'] = user
        message['To'] = ";".join(user_list)
        server = smtplib.SMTP_SSL()
        server.connect(email_host,465)
        server.login(send_user,password)
        server.sendmail(user,user_list,message.as_string())
        server.close()

if __name__ == '__main__':
    send = SendEmail()
    user_list = ['11xx@qq.com']
    sub = "测试邮件"
    content = "ceshi看看"
    send.send_mail(user_list,sub,content)

(1)Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件

(2)构造MIMEText对象时,第一个参数是邮件正文;第二个参数是MIME的subtype,传入'plain'表示纯文本,最终的MIME就是'text/plain';最后一定要用utf-8编码保证多语言兼容性

(3)发送的邮件需要添加头部信息,头部信息中包含发送者、接收者、邮件主题等信息:message['From']、message['To']、message['Subject']

(4)构造完要发送的邮件信息后,通过SMTP发出去:login()方法用来登录SMTP服务器;sendmail()方法就是发邮件,由于可以一次发给多个人,所以传入一个list;邮件正文是一个str,as_string()把MIMEText对象变成str

(5)SMTP.close() :关闭SMTP服务器连接

 

5、发送邮件带附件

参考代码:

 

#coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

class SendEmail:
    global send_user
    global email_host
    global password
    password = "lunkbrgwqxhfjgxx"
    email_host = "smtp.qq.com"
    send_user = "xx@qq.com"

    def send_mail(self,user_list,sub,content):
        user = "shape" + "<" + send_user + ">"

        # 创建一个带附件的实例
        message = MIMEMultipart()
        message['Subject'] = sub
        message['From'] = user
        message['To'] = ";".join(user_list)

        # 邮件正文内容
        message.attach(MIMEText(content, 'plain', 'utf-8'))

        # 构造附件(附件为txt格式的文本)
        att = MIMEText(open('../log/log.txt', 'rb').read(), 'base64', 'utf-8')
        att["Content-Type"] = 'application/octet-stream'
        att["Content-Disposition"] = 'attachment; filename="Log.txt"'
        message.attach(att)

        server = smtplib.SMTP_SSL()
        server.connect(email_host,465)# 启用SSL发信, 端口一般是465
        # server.set_debuglevel(1)# 打印出和SMTP服务器交互的所有信息
        server.login(send_user,password)
        server.sendmail(user,user_list,message.as_string())
        server.close()

    def send_main(self,pass_list,fail_list,no_run_list):
        pass_num = len(pass_list)
        fail_num = len(fail_list)
        #未执行的用例
        no_run_num = len(no_run_list)
        count_num = pass_num + fail_num + no_run_num

        #成功率、失败率
        '''
        用%对字符串进行格式化
        %d 格式化整数
        %f 格式化小数;想保留两位小数,需要在f前面加上条件:%.2f;用%%来表示一个%
        如果你不太确定应该用什么,%s永远起作用,它会把任何数据类型转换为字符串 
       '''
        pass_result = "%.2f%%" % (pass_num/count_num*100)
        fail_result = "%.2f%%" % (fail_num/count_num*100)
        no_run_result = "%.2f%%" % (no_run_num/count_num*100)

        user_list = ['xx@qq.com']
        sub = "接口自动化测试报告"
        content = "总共执行接口个数%s个,通过个数%s个,失败个数%s个,未执行个数%s个:通过率为%s,失败率为%s,未执行率为%s" % (count_num,pass_num,fail_num,no_run_num,pass_result,fail_result,no_run_result)
        self.send_mail(user_list,sub,content)

 

举例说明附件为TXT类型,其他类型的可参考:https://blog.csdn.net/u013250071/article/details/79037843

 

posted @ 2018-05-31 11:49  Shapelei  阅读(10435)  评论(3编辑  收藏  举报