#163邮箱发送邮件
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
my_sender = 'XXXXX@163.com'
my_pass = 'XXXXX'
my_user = 'XXXX@qq.com'
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("邮件发送失败")