import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
#发件人邮箱账号
my_sender = 'xx@126.com'
#发件人邮箱密码 密码不是真正的密码是 授权码,授权码是用于登录第三方邮件客户端的专用密码。
my_pass = 'xx'
#收件人邮箱账号,我这边发送给自己
my_user = 'xx@qq.com'
def mail():
ret = True
mail_msg = "Python 邮件发送测试"
msg = MIMEText(mail_msg, 'html', 'utf-8')
msg['From'] = formataddr(["Rocky", my_sender])
msg['To'] = formataddr(["Amy", my_user])
msg['Subject'] = '邮件主题'
server = smtplib.SMTP_SSL("smtp.126.com", 465)
server.login(my_sender, my_pass)
server.sendmail(my_sender, [my_user, ], msg.as_string())
server.quit()
ret=False
return ret
ret = mail()
if ret:
print("邮件发送成功")
else:
print("邮件发送失败")
mail()