发送简单的邮件
用一个QQ邮箱发送到另一个QQ邮件。
首先设置QQ邮箱,邮箱设置 -> 账号 开启SMTP服务,点击开启按钮,按提示进行操作,需要1毛钱的短信费。开启后如下所示
1 # coding = utf-8 2 3 import smtplib 4 from email.header import Header 5 from email.mime.text import MIMEText 6 7 # 发送邮箱 8 sender=‘12*****@qq.com‘ 9 # 发送密码,即开启smtp的授权码 10 psw=‘y****d‘ 11 12 # 接收邮箱 13 receiver=‘45*****@qq.com‘ 14 # 发送邮箱服务器 15 smtp_server=‘smtp.qq.com‘ 16 17 # 邮件正文,可编写HTML类型 18 msg=MIMEText(‘Hello,the test is finished, please check the attachment. Thanks‘,‘plain‘,‘utf-8‘) 19 20 # Header()来定义邮件标题 21 msg[‘From‘]=Header(‘Test Runner‘,‘utf-8‘) 22 msg[‘To‘]=Header(‘Test Monitor‘,‘utf-8‘) 23 msg[‘Subject‘]=Header(‘Test Result‘,‘utf-8‘) 24 25 26 server=smtplib.SMTP(smtp_server) 27 server.set_debuglevel(1) 28 server.starttls() 29 server.login(sender,psw) 30 server.sendmail(sender,receiver,msg.as_string()) 31 server.quit()
运行结果如下
发送带附件的邮件
1 #-*- coding:utf-8 -*- 2 3 import smtplib 4 from email.header import Header 5 from email.mime.text import MIMEText 6 from email.mime.multipart import MIMEMultipart 7 8 # 发送邮箱 9 sender=‘12****@qq.com‘ 10 # 发送密码,即开启smtp的授权码 11 psw=‘yzpi****cid‘ 12 13 # 接收邮箱 14 receiver=‘45*****@qq.com‘ 15 # 发送邮箱服务器 16 smtp_server=‘smtp.qq.com‘ 17 18 file = open(‘E:\\Python-Project\\unittest\\Report\\log.txt‘,‘rb‘).read() 19 att = MIMEText(file, ‘base64‘,‘utf-8‘) 20 att["Content-Type"] = ‘application/octet-stream‘ 21 att["Content-Disposition"] = ‘attachment; filename="log.txt"‘ 22 msgRoot = MIMEMultipart(‘related‘) 23 msgRoot.attach(att) 24 25 # Header()来定义邮件标题 26 msgRoot[‘From‘]=Header(‘Test Runner‘,‘utf-8‘) 27 msgRoot[‘To‘]=Header(‘Test Monitor‘,‘utf-8‘) 28 msgRoot[‘Subject‘]=Header(‘Test Result‘,‘utf-8‘) 29 30 31 server=smtplib.SMTP(smtp_server) 32 server.set_debuglevel(1) 33 server.starttls() 34 server.login(sender,psw) 35 server.sendmail(sender,receiver,msgRoot.as_string()) 36 server.quit()
运行结果
Python+Selenium学习笔记19 - 自动发送邮件
原文:https://www.cnblogs.com/sue2015/p/9142391.html