记-python发送邮件

#基于163代理的发送邮件(基于运行机器没有smtp服务)
import smtplib
import sys
from email.mime.text import MIMEText
from email.header import Header

mail_host = "smtp.163.com" # SMTP服务器
mail_user = "邮箱用户名" # 用户名
mail_pass = "XXXXX" #这里是授权码不是登陆密码不知道的可以网上搜索设置方法

sender = 'XXXX@gmail.com'
receiver = ['XXXXX@qq.com']

#参数:第一个参数为文本内容,第二个plain设置文本格式,第三个utf-8设置编码

content = 'Python Send Mail !'
title = 'Python SMTP Mail Test' # 邮件主题
message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码
message['From'] = "{}".format(sender)
message['To'] = ",".join(receiver)
message['Subject'] = title

try:
smtpObj = smtplib.SMTP_SSL(mail_host,465)
smtpObj.login(mail_user, mail_pass) # 登录验证
smtpObj.sendmail(sender, receiver, message.as_string())
print("mail send success")
except smtplib.SMTPException as e:
print(e)
posted @ 2019-11-07 13:35  leon-chan  阅读(218)  评论(0编辑  收藏  举报