gpt4 book ai didi

python - 无属性 : 'audit' found while using smtplib to send emails

转载 作者:行者123 更新时间:2023-12-01 21:49:15 25 4
gpt4 key购买 nike

我尝试通过 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'

I know i am getting error because of this line in my code:

# 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'])

当我调试我的文件时,它显示: Attribute error in module itself

任何帮助将不胜感激...

最佳答案

sys.audit()是在Python 3.8.0之后添加的,要调用此方法,您应该使用Python +3.8 Docs Audit Events

This table contains all events raised by sys.audit() or PySys_Audit() calls throughout the CPython runtime and the standard library. These calls were added in 3.8.0 or later.

smtplib Blob CPython 3.7 Github你可以看到他们没有使用sys.audit()。所以,你的问题是你的Python版本。

关于python - 无属性 : 'audit' found while using smtplib to send emails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59196222/

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