''' SMTP--即简单邮件传输协议,是一组用于从源头地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。 SMTP协议属于TCP/IP协议簇 Python内置模块对SMTP支持 smtplib--负责发送邮件 email--负责构造邮件 ''' import smtplib from email.mime.text import MIMEText from email.header import Header #定义邮件标题 #发送邮箱服务器 smtpserver='smtp.126.com' #发送邮箱的用户名密码 username='xxxxx@126.com' #使用邮箱授权码 password='xxxxxxxx' #发送和接收邮箱 sender_email='xxxxxx@126.com' receive_email='xxxxxx@qq.com' #发送邮件主题和内容 title='Selemiun 自动化测试报告' content='<html><h1 style="color:bule">测试报告内容</h1></html>' #HTML邮件正文 msg=MIMEText(content,'html','utf-8') #邮件内容 msg['Subject']=Header(title,'utf-8') #邮件标题 msg['From']=sender_email msg['To']=receive_email #SLL协议端口号要使用465 smtp=smtplib.SMTP_SSL(smtpserver,465) #向服务器标识用户身份 smtp.helo(smtpserver) #服务器返回结果确认 smtp.ehlo(smtpserver) #登录邮箱服务器用户名和密码 smtp.login(username,password) #开始发送邮件 smtp.sendmail(sender_email,receive_email,msg.as_string()) smtp.quit()
Python--发送邮件操作
最新推荐文章于 2021-11-27 10:04:41 发布