.SMTPDataError: 554, 'DT:SPM

来源:互联网 发布:北京行知学校 编辑:程序博客网 时间:2024/05/16 17:25
  1. #coding: utf-8  
  2. import smtplib  
  3. from email.mime.text import MIMEText  
  4. from email.header import Header  
  5.   
  6. sender = 'xxxx@163.com'  
  7. receiver = 'xxxx@qq.com'  
  8. subject = '放假通知'  
  9. smtpserver = 'smtp.163.com'  
  10. username = ''  
  11. password = ''  
  12.   
  13. msg = MIMEText('大家关好窗户','plain','utf-8')#中文需参数‘utf-8',单字节字符不需要  
  14. msg['Subject'] = Header(subject, 'utf-8')  
  15. msg['From'] = 'Tim<xxxx@163.com>'    
  16. msg['To'] = "xxxx@qq.com"  
  17. smtp = smtplib.SMTP()  
  18. smtp.connect('smtp.163.com')  
  19. smtp.login(username, password)  
  20. smtp.sendmail(sender, receiver, msg.as_string())  

  1. smtp.quit()  

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

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

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