基于python的邮件测试。
1、邮件授权码在此生成:
QQ邮箱--设置--账户
2、效果:
?
代码:
# -*- coding:utf-8 -*-
#引入email库
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import smtplib
# 定义发邮件
def send_mail():
#协议
smtpserver = 'smtp.qq.com'
# 设置登录邮箱的账号和授权密码
user = 'XX@qq.com'
password = "XX"
#设置发送者邮箱
sender = 'XX'
# 添加收件人的邮箱
receives = ['XX@qq.com']
# 邮件对象
msg = MIMEMultipart('mixed')
# 邮件的标题
subject = 'python邮件测试'
# 邮件正文
msg['Subject'] = Header(subject, "utf-8")
msg['From'] = sender
msg['To'] = ','.join(receives)
# 正文
text_plain = MIMEText("邮件正文测试", 'html', 'utf-8')
msg.attach(text_plain)
# ssl使用465协议端口
smtp = smtplib.SMTP_SSL(smtpserver, 465)
# 向服务器标识用户身份
smtp.helo(smtpserver)
# 向服务器返回确认结果
smtp.ehlo(smtpserver)
# 登录邮箱的账号和授权密码
smtp.login(user, password)
print("开始发送邮件...")
# 开始进行邮件的发送
smtp.sendmail(sender, receives, msg.as_string())
smtp.quit()
print("已发送邮件")
if __name__ == "__main__":
send_mail()
|