发送邮件时,报错:AttributeError: 'list' object has no attribute 'encode'

在使用腾讯企业邮箱发送邮件时出现报错:AttributeError: ‘list‘ object has no attribute ‘encode‘

 

原因:收件人不能用列表存储数据,需要转为字符串,以逗号分割

 

解决方法:

将收件人列表转为字符串,以逗号分割

to_list = [‘a@xx.com‘, ‘b@xx.com‘]
msg[‘to‘] = ‘,‘.join(to_list)

  

完整代码:

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

name = xx@xxx.cn
pwd = xxx
to_list = [xx@xxx.cn]

content = <html><head><title>test</title></head><body>这是测试邮件内容</body></html>
msg = MIMEText(content, html, utf-8)
msg[form] = Header(huyang, utf-8)
msg[to] = ,.join(to_list)                      # 重点是这个位置
msg[subject] = Header(测试邮件, utf-8)

#---发送
smtp = smtplib.SMTP_SSL(smtp.exmail.qq.com, 465)
smtp.login(name, pwd)
smtp.sendmail(name, to_list, msg.as_string())
print(发送成功!)

 

发送邮件时,报错:AttributeError: 'list' object has no attribute 'encode'

上一篇:oss上传公共方法


下一篇:Java数据结构: java.util.BitSet源码学习