python 3.7 中通过 smtp 发送电子邮件的属性错误

Attribute error in sending email through smtp in python 3.7

提问人:Sentinal 提问时间:9/27/2023 最后编辑:Sentinal 更新时间:9/27/2023 访问量:29

问:

我在这个问题中隐藏了ID和密码。但是,gmail密码是在之后生成的 开启两步验证并生成应用密码。

错误-- AttributeError:模块“smtplib”没有属性“SMTP”

import smtplib

email = "*****@gmail.com"
password = "*****"
with smtplib.SMTP("smtp.gmail.com", port=587) as connection:
    connection.starttls()
    connection.login(user=email, password=password)
    connection.sendmail(from_addr=email,
                        to_addrs="*****@yahoo.com",
                        msg="Subject:Hello\n\nThis is the body of the email"
                        )
smtp python-3.7 smtplib

评论

0赞 Goku - stands with Palestine 9/27/2023
您正在运行的 python 文件的名称是什么?
0赞 Sentinal 9/28/2023
@Goku是的,这是 email.py。在谷歌上环顾四周,发现它创建了一个循环依赖关系。我现在改了

答:

0赞 Goku - stands with Palestine 9/27/2023 #1

当 Python 发现与模块的循环依赖关系时,通常会发生此错误。smtplib

如果您的 python 脚本被命名为 或 ,则 Python 在执行库的 import 语句时会尝试导入您的文件。smtplib.pyemail.pysmtplib

因此,它会创建一个部分初始化的模块,如果您的脚本没有名为 smtp 的属性,您将遇到此错误。

解决方案

将发送 python 脚本的电子邮件重命名为除 或 以外的名称smtplib.pyemail.py