gpt4 book ai didi

Python错误email.mime没有属性 'MIMEMultipart'

转载 作者:行者123 更新时间:2023-12-01 09:32:20 25 4
gpt4 key购买 nike

我正在编写一个项目,使用 Raspberry Pi 3 通过 Gmail 发送电子邮件。该项目在我的笔记本电脑上运行得很好,但是当我尝试在 Raspberry Pi 上运行代码时,不断报告以下错误:

email.mime has no attribute 'MIMEMultipart'

我已尝试按照其他地方的建议使用 pip-install 重新安装电子邮件包,但它尚未解决问题。我特别困惑,因为它目前可以在我的笔记本电脑上运行,但不能在 Raspberry Pi 3 上运行。

我的代码如下:

import smtplib
import mimetypes
import email
import email.mime.application

FROM = "Sender's address" #This has been removed from this post, but normally contains sender's address
TO = "recipient's address" #This has been removed from this post, but normally contains recipient's address

msg = email.mime.Multipart.MIMEMultipart()
msg['Subject'] = 'Greetings'
msg['From'] = FROM
msg['To'] = TO


body = email.mime.Text.MIMEText("""hi""")
msg.attach(body)


filename= "path+filename" #e.g.(C:/Users/Pictures/pic.jpg)
fp=open(filename,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype="jpg")
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(att)

s = smtplib.SMTP('smtp.gmail.com',587)
s.starttls()
s.login('FROM','PASSWORD') #password is actually entered here in my real code, it has been removed from this post
s.sendmail('FROM',['TO'],
msg.as_string())
s.quit()

预先感谢您提供的任何指导。

最佳答案

我终于自己弄清楚了。在我的代码顶部添加导入语句解决了问题:

from email.mime.multipart import MIMEMultipart

关于Python错误email.mime没有属性 'MIMEMultipart',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49858937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com