Python--发送邮件操作

'''
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='[email protected]'
#使用邮箱授权码
password='xxxxxxxx'

#发送和接收邮箱
sender_email='[email protected]'
receive_email='[email protected]'

#发送邮件主题和内容
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()

猜你喜欢

转载自blog.csdn.net/u012002125/article/details/82392320
今日推荐