加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python3发送邮件

发布时间:2020-12-20 10:01:12 所属栏目:Python 来源:网络整理
导读:自动化测试用例上百条的时间运行时间较长,这时可以悠闲的干点别的事情,然后你也不知道什么时候会结束,但既然时自动化那必须会发邮件通知,所有我们来学习一下利用python发送邮件,后续在添加测试报告在里面。 废话不多说,上代码 1 import smtplib 2 from

自动化测试用例上百条的时间运行时间较长,这时可以悠闲的干点别的事情,然后你也不知道什么时候会结束,但既然时自动化那必须会发邮件通知,所有我们来学习一下利用python发送邮件,后续在添加测试报告在里面。

废话不多说,上代码

 1 import smtplib
 2 from email.mime.text  MIMEText
 3 from email.header  Header
 4 
 5 #发生邮箱服务器
 6 smtpserver = 'smtp.qq.com'
 7 发送用户和密码
 8 user = name@qq.com 9 password = **********10 发送邮箱
11 sender = 12 接收邮箱
13 receiver = ******@qq.com14 发送邮箱主题
15 subject = python test' 
16 编写HTML类型的邮件正文
17 msg = MIMEText(<html><h1>你好!我是python测试邮件!</h1><html>',htmlutf-8')
18 msg[Subject'] = Header(subject,1)">19 连接发送邮件
20 smtp = smtplib.SMTP()
21 smtp.connect(smtpserver)
22 smtp.login(user,password)
23 smtp.sendmail(sender,receiver,msg.as_string())
24 smtp.quit()

注意:password不要填你的密码,要填你邮箱生成的授权码。如下图点击生成授权码同时必须要开启SMTP服务哦,另外还有一个地方需要注意发送的邮箱服务器如果是qq就按我上面的填,新浪的话改成

'smtp.sina.com'其他的邮箱就改中间的标签好了。

?不然会出现如下错误

?

?经过上面的步骤终于成功发出了邮件,上图。

以上只是发送文字,那么要发送附件呢?经过又一番修改,终于也成功了。代码如下

from email.mime.multipart  MIMEMultipart
************'her_发送附件
17 sendfile = open(F:cs.txtrb).read()
18 
19 att = MIMEText(sendfile,1)">base6420 att["Content-Type"] = application/octet-stream21 att[Content-Dispositionattachment; filename="cs.txt"22 
23 msgRoot = MIMEMultipart(related24 msgRoot['] = subject
25 msgRoot.attach(att)
26 
27 28  msg = MIMEText('<html><h1>你好!我是python测试邮件!</h1><html>','html','utf-8')
29  msg['Subject'] = Header(subject,1)">30  #连接发送邮件
31 smtp =32 33 34 35 smtp.quit()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读