Sending Email with Python via SMTP and SSL

import smtplib
from email.mime.text import MIMEText

sender = 'alice@example.com'          # QQ mailbox address
auth_code = 'your-16-char-auth-code'  # generated in QQ Mail settings
recipients = ['bob@example.com', 'carol@example.com']

title = 'Test message from Python'
body = '''
Hello,
This is a plain-text message sent with smtplib.
Regards,
Alice
'''

msg = MIMEText(body, 'plain', 'utf-8')
msg['Subject'] = title
msg['From'] = sender
msg['To'] = ', '.join(recipients)

try:
    with smtplib.SMTP_SSL('smtp.qq.com', 465) as conn:
        conn.login(sender, auth_code)
        conn.sendmail(sender, recipients, msg.as_string())
    print('delivered')
except Exception as exc:
    print('failed:', exc)

HTML variant:

import smtplib
from email.mime.text import MIMEText

sender = 'alice@example.com'
password = 'your-16-char-auth-code'
target = ['bob@example.com']

subject = 'HTML demo'
html = '''
<html>
  <body>
    <h2>Python HTML Email Demo</h2>
    <p>Visit <a href="https://example.com">example.com</a></p>
  </body>
</html>
'''

mail = MIMEText(html, 'html', 'utf-8')
mail['Subject'] = subject
mail['From'] = sender
mail['To'] = ', '.join(target)

with smtplib.SMTP_SSL('smtp.qq.com', 465) as smtp:
    smtp.login(sender, password)
    smtp.sendmail(sender, target, mail.as_string())
print('sent')

Replace sender, password, and target with your own credentials and addresses.

Tags: python smtp email ssl qq-mail

Posted on Mon, 25 May 2026 18:51:37 +0000 by joquius