用Python SMTP发送我的第一封电子邮件 - 使用"with"语法无效吗?



我在网上关注一个课程,我和老师一样做。他们对这段代码使用了with块来发送电子邮件,但它给了我一个SyntaxError。我不明白我在这里做错了什么,我遵循了完全相同的步骤。

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
#Replaced email, name, & password w/ filler
message = MIMEMultipart()
message["from"] = "First_Name Last_Name"
message["to"] = "email@email.com"
message["subject"] = "This is a test"
message.attach(MIMEText("Body")
with smtplib.SMTP(host="smtp.gmail.com", port=587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.login("email@email.com", "password1234")
smtp.send_message(message)
print("Sent...")

这是我得到的错误:

File "c:UsersMofongoGoogle DriveHelloWorldapp.py", line 11
with smtplib.SMTP(host="smtp.gmail.com", port=587) as smtp:
^
SyntaxError: invalid syntax

消息.attach(MIMEText("Body"((中缺少一个右括号。

最新更新