How to open the SMTP service of QQ mail and how to use Python to send mail

How to open the SMTP service of QQ mail and how to use Python to send mail

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Come down and upload directly (img-JH8I9sJH-1689083323474)(0fe5bb9701654bf4b6652fa6d8cf6557.gif)]

Blogger profile: I am an artificial intelligence student who is studying for a postgraduate degree. I can provide services for undergraduates and graduate students related to computer and artificial intelligence. If you have any questions or confusion, please feel free to communicate!

?Motto: The sharp edge of a sword comes from sharpening, and the fragrance of plum blossoms comes from bitter cold.

Personal homepage: Click to enter blog homepage

Welcome to browse blogger’s articles! In the process of reading, if you find something that needs to be corrected, please feel free to point it out, and hope to make progress together with you!

Article directory

  • Preparation
    • SMTP service open
    • Install the email library
  • Send text mail
  • Summarize

text

Sending information by mail in Python is a very common task and it can be applied in various scenarios such as sending notifications, reports, alerts, etc. In this blog, we will introduce how to enable the SMTP service of QQ mail and how to use Python’s standard libraries smtplib and email to send emails.

Preparation

Before you start, you need to make sure you have an available email account and know the SMTP server address and port number of the email. SMTP (Simple Mail Transfer Protocol) is a protocol for sending emails. You need to know the address and port number of the SMTP server to be able to send emails using Python. The following is the SMTP server configuration information for some common mailboxes:

  • Gmail: smtp.gmail.com, port number 587
  • QQ mailbox: smtp.qq.com, port number 465
  • 163 mailbox: smtp.163.com, port number 25

SMTP service enabled

Here is an example of opening the SMTP service of QQ mailbox:
1. First enter the homepage of QQ mailbox and log in

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture directly Upload (img-YczAa8Qs-1690189544744)(image-20230724163640386.png)]

2. After entering the home page, click Settings

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture directly Upload (img-9c5uDQhD-1690189544750)(image-20230724164058183.png)]

3. Click Account

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture directly Upload (img-KAYsZ9iM-1690189544751)(image-20230724164123776.png)]

4. Scroll down and find POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV services

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image directly Upload (img-Z6ZUvZxN-1690189544752)(image-20230724164153696.png)]

5. Click Start Service, and perform QQ security verification, and then send a text message according to the prompt, the cost is about 0.1 yuan

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture directly Upload (img-lWlvuoy1-1690189544754)(image-20230724164346976.png)]

6. After completion, you will get an authorization code

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture directly Upload (img-kGzY1nRm-1690189544755)(image-20230724164530952.png)]

Having this authorization code proves that the service is enabled.

remind:

This authorization code is equivalent to the password sent by the email of the QQ account, so it should be kept well.

Install the email library

In addition, you also need to install the email standard library, which is usually installed along with the Python installation. If you don’t have it installed, you can install it with the following command:

pip install email

Send text mail

Here is a simple example program for sending emails in text format:

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

# sender email account
sender = '[email protected]'
# Sender email password or authorization code
password = 'gxnpaftaadadag'
# Recipient email account
receiver = '[email protected]'

# create mail message object
message = MIMEText('This is a test email from Python', 'plain', 'utf-8')
# Set the sender and recipients of the mail message object
message['From'] = formataddr(('Python test', sender))
message['To'] = formataddr(('recipient's name', receiver))
# Set the subject of the mail message object
message['Subject'] = 'Python test email'

try:
    # connect to SMTP server
    server = smtplib.SMTP_SSL('smtp.qq.com', 465)
    # Login email account
    server. login(sender, password)
    # send email
    server. sendmail(sender, [receiver], message. as_string())
    # Close the SMTP connection
    server. quit()
    print("Email sent successfully")
except Exception as e:
    print("Failed to send email:", e)

remind:

The above QQ authorization code is wrong, please enter the authorization code of your correct account, if you are just testing, you can private message the blogger to get the account password

In the above example, we first set the sender, password (or authorization code), and recipient’s email address. Then, we create a MIMEText object and set the content, format and encoding of the mail body. Next, we set the sender, recipient and subject of the mail message object. Finally, we connect to the SMTP server, log into the email account, send the email and close the SMTP connection.

test:

run the program

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture directly Upload (img-rIO7MSjY-1690189544757)(image-20230724164909880.png)]

[email protected] This mailbox has successfully received the mail

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture directly Upload (img-3FfBPlxG-1690189544758)(image-20230724165045020.png)]

Summary

This blog introduces how to enable the SMTP service of QQ mail and how to use Python’s standard libraries smtplib and email to send emails.

Thanks for browsing!

If this article is helpful to you, please give it a thumbs up, and hope to make progress together with you!