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 number587
- QQ mailbox:
smtp.qq.com
, port number465
- 163 mailbox:
smtp.163.com
, port number25
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
2. After entering the home page, click Settings
3. Click Account
4. Scroll down and find POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV services
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
6. After completion, you will get an authorization code
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
[email protected] This mailbox has successfully received the mail
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!