谁能告诉我为什么我收到错误信息[AttributeError:“列表"对象没有属性“编码"]

编程入门 行业动态 更新时间:2024-10-24 02:02:51
本文介绍了谁能告诉我为什么我收到错误信息[AttributeError:“列表"对象没有属性“编码"]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直尝试运行此代码,以便将Excel工作表作为附件发送到电子邮件中.我可以使用smtplib发送普通电子邮件,但无法使MIMEMultipart正常工作.我不断收到[AttributeError:'list'对象没有属性'encode']错误

I keep trying to run this code in order to send an excel sheet as an attachment on an email. I can send normal emails using smtplib but can't get the MIMEMultipart to work. I keep getting the [AttributeError: 'list' object has no attribute 'encode'] error

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', 587) smtpObj.ehlo() smtpObj.starttls() smtpObj.login('email', 'password') text = msg.as_string() smtpObj.sendmail(fromaddr, sendto , text) smtpObj.quit()

推荐答案

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

这对我来说有点奇怪.它们不应该是字符串,不是列表吗?

This looks a little odd to me. Shouldn't they be strings, not lists?

fromaddr = 'Email' sendto = 'Email'

更多推荐

谁能告诉我为什么我收到错误信息[AttributeError:“列表"对象没有属性“编码"]

本文发布于:2023-11-03 20:15:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:告诉我   谁能   错误信息   属性   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!