没有属性“SMTP",尝试在 Python 中发送电子邮件时出错 [英] No attribute 'SMTP', error when trying to send email in Python
问题描述
我正在尝试用 Python 发送电子邮件:
I am trying to send an email in Python:
import smtplib
fromaddr = '......................'
toaddrs = '......................'
msg = 'Spam email Test'
username = '.......'
password = '.......'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
我知道这可能不是正确的消息格式.
I understand that this is probably not the correct message format.
无论如何,我得到一个错误:
Anyways, I get an error:
C:.....>python email.py
Traceback (most recent call last):
File "email.py", line 1, in <module>
import smtplib
File "C:.....Pythonlibsmtplib.py", line 47,
in <module>
import email.utils
File "C:.....email.py", line 15, in
<module>
server = smtplib.SMTP('smtp.gmail.com', 587)
AttributeError: 'module' object has no attribute 'SMTP'
我不太明白我在这里做错了什么......有什么不正确的吗?
I don't quite understand what I am doing wrong here... Anything incorrect?
注意:所有句点都是密码/电子邮件/文件路径/等的替换.
NOTE: All the periods are replacements for password/email/file paths/etc.
推荐答案
Python 已经有了 email
模块.您的脚本名称是 email.py
,这会阻止 smtplib
导入内置的 email
模块.
Python already has an email
module. Your script's name is email.py
, which is preventing smtplib
from importing the built-in email
module.
将您的脚本重命名为 email.py
以外的其他名称,问题就会消失.
Rename your script to something other than email.py
and the problem will go away.
这篇关于没有属性“SMTP",尝试在 Python 中发送电子邮件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!