Python ------ use QQ mailbox to send plain text mail

This article introduces how to use Python environment to realize plain text sending of QQ mailbox

The authorization code of QQ email is needed here. First, the access method of authorization code is introduced:

Open QQ email: click settings and select account

There is an open service under the account. After the OPO3/SMTP service, click Open (because my is open, it can be closed, and the pop-up box doesn't need to be checked)

Click to open the following page. Just do it. (the arrow of the second picture below points to the authorization code.)

  • First edition

The steps are as follows:

  1. Set message content
  2. Use MIMEText to convert message content (string) into message form and store
  3. Set message subject
  4. Set sender
  5. Set mail server and port number
  6. Login mailbox
  7. Send mail
  8. Exit after sending
import smtplib
from email.mime.text import MIMEText

#Set message content
content="Two Orioles are singing green willows, and a line of egrets is flying in the sky"
#Use MIMEText to convert message content to message form and store
message=MIMEText(content)
#Set message subject
message["subject"]="four-line poem with seven characters per line"
#Set sender
message["From"]="Sender QQ mailbox"
#Set mail server and port number
smtp_email=smtplib.SMTP("smtp.qq.com",25)
#Log in the email, QQ email uses QQ account and authorization code
smtp_email.login(user="fill in QQ mailbox",password="Fill in authorization code")
#Send mail, send mail is to set the sender, recipient and mail content
smtp_email.sendmail(from_addr="Sender QQ mailbox",to_addrs="Addressee QQ mailbox",msg=message.as_string())
#Exit after sending
smtp_email.quit()
#Print sent successfully
print("Send successfully")

Print "send successfully" when sending is successful

  • The second edition

The difference with the first version is to separate the sender's QQ number, the recipient's QQ number and the mailbox server, so as to avoid filling in numbers many times and prevent errors at the same time. At the same time, the function of bombing transmission is added.

The steps are as follows:

  1. Import two operation modules, smtplib and mime text
  2. Mime text converts strings to message format from email.mime.text import MIMEText
  3. You can set the email server on Baidu search: qq,163, Ali, etc. qq:smtp.qq.com
  4. Set up email account
  5. Set the email authorization code or password qq as the authorization code '***********'
  6. Mail content setting string assignment
  7. The string of the previous step is converted to mail form and stored through the MIMEText function
  8. message['subject '] set subject
  9. message[From '] set sender
  10. Create sending mail object smtplib.SMTP (parameter: mailbox server, encryption method: default port 25)
  11. #Login server (parameter: account, password) mail object.login
  12. Send mail (who sent it, who sent it, and turn the mail object into a specific string). sendmail(mail_user,to,message.as_string())
  13. Quit() disconnect message
import smtplib
from email.mime.text import MIMEText
import time
def send_mail():
    # Set up the mail server. This code uses QQ mailbox to send, so the mail service is smtp.qq.com
    sever="smtp.qq.com"
    #Sender account
    user="Sender QQ mailbox"
    #Authorization code of sender qq
    pwd="Sender QQ Email authorization code"
    #Specific content sent in the mailbox
    content="Two Orioles are singing green willows, and a line of egrets is flying in the sky"
    # The resulting string is converted to mail form through the MIMEText function and stored
    message=MIMEText(content)
    #Set message subject
    message["subject"]="four-line poem with seven characters per line"
    #Set sender of message
    message["From"]=user
    #Set up recipients
    to="Addressee QQ mailbox"
    #Define mailbox server and port number
    smtp_email=smtplib.SMTP(sever,25)
    #Login account
    smtp_email.login(user=user,password=pwd)
    #Send mail
    smtp_email.sendmail(from_addr=user,to_addrs=to,msg=message.as_string())
    #Close
    smtp_email.quit()
    print("Send successfully")
while True:
    send_mail()
    time.sleep(10)

 

 

 


 

 

 

 

Published 7 original articles, won praise 9, visited 2226
Private letter follow

Tags: Python

Posted on Sun, 02 Feb 2020 00:45:34 -0800 by PhpMachine