SoFunction
Updated on 2024-11-07

An introduction to Python's authorization code for sending emails with QQ mailboxes

QQ mailbox has newly launched an authorization code, which requires the verified cell phone number to send a text message to QQ mailbox server to obtain. The authorization code is used for third-party client login, replacing the personal mailbox password used for third-party login.

Two problems were encountered during testing:

1. Prompted to establish SSL secure connection. So I changed () to smtplib.SMTP_SSL().

2. After running the code, the program has been running, but there is no response, waiting for about five minutes, and finally had to ctrl + c stop. Check the reason to know, QQ mailbox SMTP service port is not the default 25. changed to 465 after it is good.

(When connecting to an SMTP server using the standard port 25, a clear text transmission is used and the entire process of sending an email can be eavesdropped on. To send emails more securely, you can encrypt the SMTP session, in effect creating an SSL secure connection before sending emails using the SMTP protocol.)

3.Run prompted 535 error, said authorizationerror. only to know is the reason of that authorization code. So I hurried to get the authorization code of the mailbox.

3.Run prompted 535 error, said authorizationerror. only to know is the reason of that authorization code. So I hurried to get the authorization code of the mailbox.

The final test is finally good and the code is below:

#coding:utf-8
import smtplib
from  import MIMEText
from  import Header
# Third-party SMTP services
mail_host="" # Setting up the server
mail_user="******@" #Username
mail_pass="*********" # password, QQ mailbox is to enter the authorization code, in the qq mailbox settings in the verified cell phone to send a text message to get, does not contain spaces

sender = '******'
receivers = ['******@','****@'] # Receive emails, can be set to your QQ mailbox or other mailboxes
message = MIMEText('a test for python', 'plain', 'utf-8')
message['From'] = Header("ppyy", 'utf-8')
message['To'] = Header("you", 'utf-8')
subject = 'my test'
message['Subject'] = Header(subject, 'utf-8')
try:
 smtpObj = smtplib.SMTP_SSL(mail_host, 465) 
 (mail_user,mail_pass) 
 (sender, receivers, message.as_string())
 ()
 print u"Mail sent successfully."
except ,e:
 print e

The above this talk about Python with QQ mailbox to send mail when the authorization code is all that I have shared with you, I hope to be able to give you a reference, but also hope that you support me more.