python 使用smtp 带附件发送一致报错,求救!!!

牛奶不太白 2019-03-21 08:02:49
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import getpathInfo
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

mail_host = 'smtp.qq.com'
mail_user = '609176127'
mail_pass = ''
mail_path = os.path.join(getpathInfo.get_Path(),'result','report.html')


def send_mailss():
sender = '609176127@qq.com'
receiver = [ 'cen646@163.com' ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
subject = '测试目的'

message = MIMEMultipart()
message['From'] = sender # 发送者
message['To'] = receiver
message['Subject'] = subject

# message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')
# print(message)
att1 = MIMEText(open(mail_path,'rb').read(),'base64','utf-8')
att1['Content-Type']='application/octet-stream'
att1['Content-Disposition']='attachment;filename=mail_path '
#
message.attach(att1)
# try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host,25)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receiver, message.as_string())
print("success")
smtpObj.quit()

if __name__ == '__main__':
send_mailss()


————————————————————————————————————————————————

报错区

Traceback (most recent call last):
File "D:/project_py/InterfaceAutoFrame/app.py", line 41, in <module>
send_mailss()
File "D:/project_py/InterfaceAutoFrame/app.py", line 36, in send_mailss
smtpObj.sendmail(sender, receiver, message.as_string())
File "c:\users\60917\appdata\local\programs\python\python37\Lib\email\message.py", line 158, in as_string
g.flatten(self, unixfrom=unixfrom)
File "c:\users\60917\appdata\local\programs\python\python37\Lib\email\generator.py", line 116, in flatten
self._write(msg)
File "c:\users\60917\appdata\local\programs\python\python37\Lib\email\generator.py", line 195, in _write
self._write_headers(msg)
File "c:\users\60917\appdata\local\programs\python\python37\Lib\email\generator.py", line 222, in _write_headers
self.write(self.policy.fold(h, v))
File "c:\users\60917\appdata\local\programs\python\python37\Lib\email\_policybase.py", line 326, in fold
return self._fold(name, value, sanitize=True)
File "c:\users\60917\appdata\local\programs\python\python37\Lib\email\_policybase.py", line 369, in _fold
parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))
AttributeError: 'list' object has no attribute 'encode'
...全文
169 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧