开通QQ邮箱授权码
代码
import smtplib
from email.mime.text import MIMEText #以上库python自带
# 服务器信息
mail_host = 'smtp.qq.com'
mail_port = 465
mail_user = '30714261xx@qq.com' #邮箱账号
mail_pass = 'qfxisiwbaofuddia' #密码(QQ邮箱为授权码)
sender = '3071426176@qq.com' #邮件发送方邮箱地址
receivers = ['204825241@qq.com'] #邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发
#设置email信息
content = 'This is a test email comes from python' #邮件内容
message = MIMEText(content,'plain','utf-8')
message['Subject'] = 'send email by python' #邮件主题
message['From'] = sender #发送方信息
message['To'] = receivers[0] #接受方信息
#登录并发送邮件
try:
s = smtplib.SMTP_SSL(mail_host, mail_port) #登录到服务器
s.login(mail_user,mail_pass) #登录邮箱
s.sendmail(sender,receivers,message.as_string()) #发送
s.quit() #退出
print('success')
except smtplib.SMTPException as e:
print('error',e) #打印错误