使用smtplib发送电子邮件时找不到属性:“audit”

2024-05-03 18:15:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

任何帮助都将不胜感激。。。你知道吗


Tags: thenameinfromcommessageyourserver
1条回答
网友
1楼 · 发布于 2024-05-03 18:15:49

系统审计()添加在Python 3.8.0之后,要调用此方法,应使用Python+3.8Docs 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中,您可以看到他们没有使用系统审计(). 所以,你的问题在于你的Python版本。你知道吗

相关问题 更多 >