How to send and receive email with python
This article will explain in detail how to send and receive email on python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. Install the mail library.
Pip install PyEmail
2. For the email interface, you need to enable the mailbox configuration-SMTP service in the account.
Next, we will get the authorization code by sending SMS verification, which can be added to the code after having the authorization code.
3. Set login information, send content, etc., open smtp service and connect to the server. Log in to the mailbox to send or receive mail, and finally shut down the service.
Example
Import smtplibfrom email.mime.text import MIMETextfrom email.header import Headerimport time # sender's information: email address, QQ Mail authorization code from_addr = "xxx@qq.com" password = "xxxx" # recipient mailbox to_addr = "xxx@qq.com" # sender server smtp_server = "smtp.qq.com" # mailbox body content, the first parameter is content, and the second parameter is format (plain is plain text) The third parameter is to encode msg = MIMEText ('Robben Shuai', 'plain','utf-8') # header message msg [' From'] = Header (from_addr) msg ['To'] = Header (to_addr) msg [' Subject'] = Header ('python') # enable sending service Encrypted transmission server = smtplib.SMTP_SSL (smtp_server) server.connect (smtp_server,465) # Log in to send email server.login (from_addr,password) for i in range (2): # send email server.sendmail (from_addr,to_addr) Msg.as_string () time.sleep (1) server.quit () # shuts down the server. That's all for the article on "how to send and receive email with python". Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.