smtplib.SMTPDataError: 554, 'DT:SPM

#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = '[email protected]'
receiver = '[email protected]'
subject = '放假通知'
smtpserver = 'smtp.163.com'
username = ''
password = ''

msg = MIMEText('大家关好窗户','plain','utf-8')#中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'Tim'  
msg['To'] = "[email protected]"
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()


只赋值了msg['Subject'], 没有赋值msg['From'], msg['To'],导致出现554情况,更改了后就好了。

注意MIMEText初始化的时候,中文的第二个参数要用'plain',我用'text',中文就显示不出来。

msg['From'] 中人名为Tim,会和下面对应起来。


你可能感兴趣的:(python学习笔记)