16.python邮件发送
1、SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议
它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
2、smtplib就是对smtp的一种简单封装
3、基于文本邮件的发送
# 实现对邮件进行发送
import smtplib
#email实现邮件构建
from email.mime.text import MIMEText
from email.header import Header
# 如何实现文本邮件的发送,plain指的就是纯文本
message=MIMEText(_text="python 邮件发送测试.....",_subtype='plain',_charset="utf-8")
message["From"]=Header("素素01","utf-8")
message["To"]=Header("素素02","utf-8")
message["Subject"]=Header("python email send test","utf-8")
smtpobj=smtplib.SMTP()
# qq的smtp服务器地址
mail_host="smtp.qq.com"
try:
# 连接smtp服务器
smtpobj.connect(host=mail_host,port="587")
# 用户登录,用户名即为发送者地址,密码不是账号的密码,是授权码
smtpobj.login(user="2804555260@qq.com",password="sfvcuietpckxddga")
# 如何获取授权码 发送者邮件点击设置-账户-开启pop3/smtp协议 获取授权码
# sfvcuietpckxddga
sender="2804555260@qq.com"
receiver=['2804555260@qq.com']
# 实现邮件发送
smtpobj.sendmail(sender,receiver,message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("邮件发送失败!")
4、基于html邮件发送
# 发送html格式邮件
import smtplib
#email实现邮件构建
from email.mime.text import MIMEText
from email.header import Header
# 如何实现文本邮件的发送,plain指的就是纯文本,html文件
html="""
<p>python 邮件发送测试</p>
<p><a href="https://www.baidu.com">百度</a>
</p>
"""
message=MIMEText(_text=html,_subtype='html',_charset="utf-8")
message["From"]=Header("素素01","utf-8")
message["To"]=Header("素素02","utf-8")
message["Subject"]=Header("python email send test","utf-8")
smtpobj=smtplib.SMTP()
# qq的smtp服务器地址
mail_host="smtp.qq.com"
try:
# 连接smtp服务器
smtpobj.connect(host=mail_host,port="587")
# 用户登录,用户名即为发送者地址,密码不是账号的密码,是授权码
smtpobj.login(user="2804555260@qq.com",password="sfvcuietpckxddga")
# 如何获取授权码 发送者邮件点击设置-账户-开启pop3/smtp协议 获取授权码
# sfvcuietpckxddga
sender="2804555260@qq.com"
receiver=['2804555260@qq.com']
# 实现邮件发送
smtpobj.sendmail(sender,receiver,message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("邮件发送失败!")
5、发送带附件邮件
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
message=MIMEMultipart()
message.attach(MIMEText('这是菜鸟教程python邮件发送测试...',_subtype="plain",
_charset="utf-8"))
att1=MIMEText(open('test.txt',"rb").read(),"base64","utf-8")
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="1.txt"'
message.attach(att1)
message["From"]=Header("素素01","utf-8")
message["To"]=Header("素素02","utf-8")
message["Subject"]=Header("python 发送带附件邮件","utf-8")
# 继续添加多个文件为附件
att2=MIMEText(open('2.txt',"rb").read(),"base64","utf-8")
att2["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att2["Content-Disposition"] = 'attachment; filename="2.txt"'
message.attach(att2)
try:
smtpobj=smtplib.SMTP()
# 连接smtp服务器
smtpobj.connect(host="smtp.qq.com", port="587")
# 用户登录,用户名即为发送者地址,密码不是账号的密码,是授权码
smtpobj.login(user="2804555260@qq.com", password="sfvcuietpckxddga")
sender = "2804555260@qq.com"
receiver = ['2804555260@qq.com']
smtpobj.sendmail(sender,receiver,message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("邮件发送失败")