Smtp problem

raise SMTPResponseException(code, message)
smtplib.SMTPResponseException: (-1, b’\x00\x00\x00’)

I use QQ mailbox to send errors or even send more requests to add “server.quit()”, which is more stable

#!/usr/bin/env python3
import smtplib
import ssl
import sys
from email.mime.text import MIMEText
from email.utils import formataddr

配置

SMTP_SERVER = “smtp.qq.com”
SMTP_PORT = 465 # SSL 直连
SENDER = “”
PASSWORD = “” # SMTP授权码
RECEIVER = “”

def send_mail(subject, content):
msg = MIMEText(content, “plain”, “utf-8”)
msg[“From”] = formataddr((“TrueNAS”, SENDER))
msg[“To”] = formataddr((“User”, RECEIVER))
msg[“Subject”] = subject

context = ssl.create_default_context()
try:
    server = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT, context=context)
    server.login(SENDER, PASSWORD)
    server.sendmail(SENDER, [RECEIVER], msg.as_string())
    server.quit()  # 显式关闭连接,Linux 下更稳定
    print("邮件已发送成功 ✅")
except Exception as e:
    print("发送邮件失败 ❌:", e)

if name == “main”:
if len(sys.argv) < 3:
print(“用法: sendmail.py ‘主题’ ‘内容’”)
sys.exit(1)
subject = sys.argv[1]
content = sys.argv[2]
send_mail(subject, content)

Here you find some info and how i handle this QQmail “problem” in my sendemail (basically i have to ignore those kind of exception).
Take a look at my script, grab It and use It if you prefere :wink:

smtp.send_message(msg)
smtp.quit()

Just add quit

In my script i have used the context manager sintax

with smtplib.SMTP_SSL(..., timeout=30) as server:

that should implicitly send the server.quit command, but i get reported that the same exception where raised anyway, and this happening only with QQmail.
I think that the real difference Is in this line of your code

context = ssl.create_default_context()

But i need to look into that deeper to avoid regressions, but hey, you give me a really good point to start