## Sending Email
import smtplib
from email.mime.text import MIMEText
# 第三方SMTP服务
mail_host = 'smtp.126.com'
mail_user = 'example@126.com'
mail_pass = 'password' # 客户端授权密码,非登录密码
sender = 'example@126.com'
receivers = ['example@qq.com']
content = 'Python Send Mail !'
title = 'Python SMTP Mail Test'
message = MIMEText(content, 'plain','utf-8')
message['From'] = '{}'.format(sender)
message['To']=','.join(receivers)
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.set_debuglevel(1) # 解决 500 Error: bad syntax
smtpObj.ehlo(mail_host)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
except smtplib.SMTPException as e:
print(e)
问题解决:
1. 报错:500 Error: bad syntax
执行smtpObj.set_debuglevel(1)
参考:https://blog.youkuaiyun.com/DearMorning/article/details/81069075