一次性解决python smtp 发送outlook邮件,163邮件,qq邮件等等.
市场上的 smtp服务 无非2类
1.直接发的.用mail 登录密码. 如 outlook,qq邮箱
2.需要设置smtp 独立密码. 如 163邮件
python使用两种发邮件的方式smtp和outlook示例
本篇文章主要实现了python使用两种发邮件的方式smtp和outlook示例
smtp是直接调用163邮箱的smtp服务器,需要在163邮箱中设置一下。
outlook发送就是同样是在outloog 的设置中搜索即可
smtp 163 配置
自己看去.
主要163 需要你申请一个 授权密码. 代码中输入的密码就是你的授权密码
smtp outloook 配置查看
import smtplib #smtp服务器
from email.mime.text import MIMEText #邮件文本
from store import ds, views_redis
# import win32com.client as win32
# import xlrd
from email.header import Header
#邮件构建 outlook邮件
def send_email(subject="24zbw推送提醒",content="24zbw推送集锦录像失败,请查看",recver="test@outlook.com"):
# 第三方 SMTP 服务
mail_host="smtp.office365.com" #设置服务器
mail_user="test@outlook.com" #用户名
mail_pass="123456" #口令
sender = 'test@outlook.com'
# receivers = ['test@qq.com']
# 调试开启 写死 我的邮件
receivers = recver # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
message = MIMEText(content, 'plain', 'utf-8')
# message['From'] = Header("24脚本", 'utf-8')
# message['To'] = Header("24脚本接收端", 'utf-8')
# subject = subject
message['Subject'] = Header(subject, 'utf-8')
# try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 587) # 25 为 SMTP 端口号
# 必须先登录
smtpObj.ehlo() # 用户认证
smtpObj.starttls() # 明文通信协议的扩展,能够让明文的通信连线直接成为加密连线(使用SSL或TLS加密),而不需要使用另一个特别的端口来进行加密通信,属于机会性加密
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print('邮件发送成功')
# except smtplib.SMTPException:
# print('Error: 无法发送邮件')
# send_email()
# #邮件构建 163邮件
# def send_email(subject="24zbw推送集锦录像失败,请查看",content="24zbw推送集锦录像失败,请查看",recver="test@outlook.com"):
# # subject = "24zbw推送集锦录像失败,请查看"#邮件标题
# sender = "test@outlook.com"#发送方
# # content = "24zbw推送集锦录像失败,请查看"
# # recver = "test@qq.com"#接收方
# password = "123456"#邮箱密码
# message = MIMEText(content,"plain","utf-8")
# #content 发送内容 "plain"文本格式 utf-8 编码格式
# message['Subject'] = subject #邮件标题
# message['To'] = recver #收件人
# message['From'] = sender #发件人
# # try:
# smtp = smtplib.SMTP_SSL("smtp.office365.com",587) #实例化smtp服务器
# smtp.login(sender,password)#发件人登录
# smtp.sendmail(sender,[recver],message.as_string()) #as_string 对 message 的消息进行了封装
# smtp.close()
# # except smtplib.SMTPException:
# # print('smtplib.SMTPException: ', smtplib.SMTPException)
# # print ("Error: 邮件异常,请注释掉except查看。无法发送邮件")
发送成功.收到邮件
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: