【发布时间】:2020-03-30 10:45:40
【问题描述】:
我试图通过smtplib发送电子邮件,但我总是收到属性错误。错误是:
Traceback (most recent call last):
File "C:\Users\intel\Desktop\Python\test.py", line 24, in <module>
server = smtplib.SMTP('smtp.gmail.com', 587)
File "C:\Users\intel\Desktop\Python\smtplib.py", line 195, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\intel\Desktop\Python\smtplib.py", line 275, in connect
sys.audit("smtplib.connect", self, host, port)
AttributeError: module 'sys' has no attribute 'audit'
我知道我的代码中出现了错误:
# Step 7 - Create the server connection server = smtplib.SMTP('smtp.gmail.com', 587)
这是我的代码:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
# Step 2 - Create message object instance
msg = MIMEMultipart()
# Step 3 - Create message body
message = "Test from Python via AuthSMTP"
# Step 4 - Declare SMTP credentials
password = "password"
username = "andrew.whiteman77@gmail.com"
# Step 5 - Declare message elements
msg['From'] = "your.name@your-domain-name.com"
msg['To'] = "your.name@your-domain-name.com"
msg['Subject'] = "Test from Python via AuthSMTP"
# Step 6 - Add the message body to the object instance
msg.attach(MIMEText(message, 'plain'))
# Step 7 - Create the server connection # here is the line
server = smtplib.SMTP('smtp.gmail.com', 587) # where i am getting error!!!
# Step 8 - Switch the connection over to TLS encryption
server.starttls()
# Step 9 - Authenticate with the server
server.login(username, password)
# Step 10 - Send the message
server.sendmail(msg['From'], msg['To'], msg.as_string())
# Step 11 - Disconnect
server.quit()
# Step 12 -
print("Successfully sent email message to %s:") % (msg['To'])
任何帮助将不胜感激...
【问题讨论】:
-
您能分享一下您用于该运行的 smtplib 版本吗?
-
我不太清楚。当我尝试使用 pip 安装 smtplib 时它不存在,所以我只是复制了它的源代码并粘贴到一个新文件中,即 smtplib.py。任何帮助将不胜感激,我想这是最新的。
-
嗯,这是官方网站源代码的链接:github.com/python/cpython/blob/3.8/Lib/smtplib.py
-
我在 Pythın 3.7.3 和最新版本的 smtplib 中没有遇到任何错误。我什至收到了来自 Gmail 的安全警告。
-
您的代码没有错误,您可以尝试重新安装/升级您的 Python 发行版。
标签: python python-3.x smtp attributeerror smtplib