任何人都可以告诉我为什么我得到错误

我一直试图运行这个代码,以发送Excel表格作为电子邮件的附件。 我可以使用smtplib发送正常的电子邮件,但无法使MIMEMultipart正常工作。 我不断收到[AttributeError:'列表'对象没有属性'编码']错误

import smtplib, ssl from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email import encoders fromaddr = ['Email'] sendto = ['Email'] msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = sendto msg['Subject'] = 'This is cool' body = "this is the body of the text message" msg.attach(MIMEText(body, 'plain')) filename = 'Work.xlsx' attachment = open('/home/mark/Work.xlsx', 'rb') part = MIMEBase('application', "octet-stream") part.set_payload((attachment).read()) encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename= %s' % filename) msg.attach(part) smtpObj = smtplib.SMTP('smtp.gmail.com', 587) smtpObj.ehlo() smtpObj.starttls() smtpObj.login('email', 'password') text = msg.as_string() smtpObj.sendmail(fromaddr, sendto , text) smtpObj.quit() 

 fromaddr = ['Email'] sendto = ['Email'] 

这对我来说有点奇怪。 他们不应该是string,而不是列表?

 fromaddr = 'Email' sendto = 'Email'