#163邮箱发送邮件
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

发件人邮箱账号

my_sender = '[email protected]'

发件人邮箱密码 密码不是真正的密码是 授权码,授权码是用于登录第三方邮件客户端的专用密码。

my_pass = 'XXXXX'

收件人邮箱账号,我这边发送给自己

my_user = '[email protected]'

def mail():
ret = True
try:
mail_msg = "Python 邮件发送测试"
msg = MIMEText(mail_msg, 'html', 'utf-8')
msg['From'] = formataddr(["Rocky", my_sender])
msg['To'] = formataddr(["Amy", my_user])
msg['Subject'] = '邮件主题'
'''
#邮箱 发件服务器 非SSL协议端口 SSL协议端口
#163 smtp .163.com 25 465 / 587
#qq smtp.qq.com 25 465 / 587
'''
server = smtplib.SMTP_SSL("smtp.163.com", 465)
server.login(my_sender, my_pass)
server.sendmail(my_sender, [my_user, ], msg.as_string())
server.quit()
except smtplib.SMTPException:
ret=False

return ret

ret = mail()
if ret:
print("邮件发送成功")
else:
print("邮件发送失败")