# coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import ssl
# 第三方 SMTP 服务
mail_host="smtp.office365.com" #设置服务器
mail_user="xxxx@office2016.com" #用户名
mail_pass="xxxxx" #口令
sender = "xxxx@office2016.com"
receivers = ["xxxxxx@qq.com"] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
message = MIMEText('测试已完成', 'plain', 'utf-8')
try:
smtp = smtplib.SMTP(mail_host)
smtp.connect(mail_host, 587) #此处端口号请在邮件提供商处查找
smtp.starttls(context=ssl.create_default_context())#对于使用starttls加密的,必须使用这个通过认证
smtp.login(mail_user,mail_pass)
print("登录成功")
print(message.as_string())
smtp.sendmail(sender, receivers, message.as_string())
print ("邮件发送成功")
smtp.quit()
except smtplib.SMTPException as e:
smtp.quit()
print('error',e)
python 3.8 发送邮件
最新推荐文章于 2023-03-28 23:49:50 发布