flask发送qq邮件报错smtplib.SMTPServerDisconnected: please run connect() first
时间: 2024-04-10 14:33:50 浏览: 23
您好!出现这个错误是因为在发送 QQ 邮件之前没有先建立与 SMTP 服务器的连接。您需要在发送邮件之前先调用 `connect()` 方法来建立连接。以下是一个示例代码:
```python
import smtplib
from email.mime.text import MIMEText
# 邮件配置
smtp_server = 'smtp.qq.com'
smtp_port = 465
sender = 'your_email@qq.com'
password = 'your_password'
receiver = 'recipient_email@qq.com'
# 建立与 SMTP 服务器的连接
smtp_obj = smtplib.SMTP_SSL(smtp_server, smtp_port)
smtp_obj.login(sender, password)
# 构造邮件内容
message = MIMEText('Hello, this is a test email.')
message['From'] = sender
message['To'] = receiver
message['Subject'] = 'Test Email'
# 发送邮件
smtp_obj.sendmail(sender, receiver, message.as_string())
# 关闭连接
smtp_obj.quit()
```
请确保将上述代码中的 `your_email@qq.com` 替换为您的 QQ 邮箱地址,`your_password` 替换为您的 QQ 邮箱密码,`recipient_email@qq.com` 替换为接收邮件的邮箱地址。
希望能解决您的问题!如果还有其他疑问,请随时提问。