Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the function of sending and receiving email automatically in Python

Shulou Source: shulou.com Published: 2022-06-02 05:41:40 10月04日 Update

It is believed that many inexperienced people are at a loss about how to realize the function of automatically sending and receiving email in Python. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Project environment

Language: Python

Editor: Pycharm

Libraries: smtplib, email

Code analysis

Let's first set up the sending account and receiving account of the email. As variables, I use the QQ account here, and the sender is my second account.

My_sender = 'xxxx@qq.com' # sender's email account

My_user = 'xxxx@qq.com' # recipient email account

Basic Information Settin

Next, we set up some basic information about the mailbox and login operations. The sent text is processed into Chinese encoding using the MIMEText method. Set information such as the subject of the message and the sender of the message.

Log in to the mailbox

Login mailbox we need to define the mail transfer protocol, the general mail transfer protocol is SMTP,QQ mailbox is smtp.qq.com, NetEase mailbox is smtp.qq.com, other mailboxes can be checked on the Internet. The port defaults to 25. Log in to the mailbox with the login method, the first parameter is the login account, and the second parameter note that the password here is not the login password of the mailbox, but the authorization code of the mailbox, which can be viewed in the mailbox settings. The following is QQ Mail's viewing method. Other mailbox viewing methods are similar.

After the login is successful, use the sendmail method to send the email, and then exit the email.

Finally, enter the title and content parameters of sending mail, and then call the function to realize the function of sending and receiving mail.

Mail_title = 'report'

Mail_content = 'report content'

SendMail (mail_title, mail_content)

After reading the above, have you mastered the method of how to automatically send and receive email in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Mailbox email method login account content function information parameter password report more problem transmission success helplessness for this next subject code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS Shulou Information Huawei Microsoft