How to Configure QQ Email SMTP and Send Emails with Python
This guide explains how to find QQ mail's POP3/SMTP server settings, enable the SMTP service in the QQ account, and provides a complete Python script that uses smtplib to send an email through QQ's SMTP server.
1. Understand QQ Email SMTP
QQ email POP3 and SMTP server addresses are as follows:
POP3 Server (Port 995)
SMTP Server (Port 465 or 587)
qq.com
pop.qq.com
smtp.exmail.qq.com
The SMTP server requires authentication.
2. Enable QQ Email SMTP Service
How to turn on POP3/SMTP/IMAP?
To protect user security, QQ email disables POP3/SMTP/IMAP by default; you need to enable them when required. First, log in to the mailbox and go to Settings → Account.
Then, in the Account settings, locate the toggle items and enable them as shown below:
Finally, save the settings to activate the corresponding services.
3. Code
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
my_sender = '[email protected]' # sender email address
my_pass = 'xxxxxx' # sender email password (SMTP token)
my_user = '[email protected]' # recipient email address (sending to self)
def mail():
ret = True
try:
msg = MIMEText('填写邮件内容', 'plain', 'utf-8')
msg['From'] = formataddr(["发件人昵称", my_sender]) # sender nickname and address
msg['To'] = formataddr(["收件人昵称", my_user]) # recipient nickname and address
msg['Subject'] = "邮件主题-测试" # email subject
server = smtplib.SMTP_SSL("smtp.exmail.qq.com", 465) # SMTP server and port
server.login(my_sender, my_pass) # authenticate
server.sendmail(my_sender, [my_user], msg.as_string()) # send email
server.quit() # close connection
except Exception:
ret = False
return ret
ret = mail()
if ret:
print("邮件发送成功")
else:
print("邮件发送失败")Note: If the recipient address is incorrect, the script may still print “邮件发送成功”, but QQ will return a bounce message indicating delivery failure.
Follow the QR code below to get more automation learning resources.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.