python3 smtplib

import smtplib
from email.mime.text import MIMEText
from email.header import Header

#smtp服务

mail_host = "smtp.qq.com"
mail_user = "[email protected]"
mail_pass = "11111111"

sender = '[email protected]'
receivers = ['[email protected]']

message = MIMEText('邮件发送测试。。。','plain','utf-8')
message['From'] = Header("python学习",'utf-8')
message['TO'] = Header("测试",'utf-8')

subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject,'utf-8')

try:
server = smtplib.SMTP()
server.connect(mail_host,25)
server.login(mail_user,mail_pass)
server.sendmail(sender,receivers,message.as_string())
print("send mail successful")
# except Exception as e:
except smtplib.SMTPException as e:
print("Error;send mail fail")
print(e)

猜你喜欢

转载自www.cnblogs.com/gaoyanbing/p/12710646.html
今日推荐