The examples in this article share the specific code of python to send emails by QQ mailbox for your reference. The specific content is as follows
**1、 Code: **
#! /usr/bin/python
# - *- coding: utf-8-*-import smtplib
from email.mime.text import MIMEText
from email.header import Header
mail_host="smtp.qq.com"#The set mail server host must be the server of the sending mailbox and has nothing to do with the receiving mailbox.
mail_user="**************"#QQ mailbox login name
mail_pass="*****************" #The authorization code set when the stmp service is turned on, pay attention! Not a QQ password.
sender='[email protected]'#Sender qq mailbox
receivers=['[email protected]']#Recipient qq mailbox
message=MIMEText('Test sending python mail','plain','utf-8')
message['From']=Header("beibei",'utf-8') #Set the sender shown in the mail
message['To']=Header("wowo",'utf-8') #Set the recipients shown in the email
subject ='python smtp email test'
message['Subject']=Header(subject,'utf-8') #Set theme and format
try:
smtpobj=smtplib.SMTP_SSL(mail_host,465) #If there is a local server, use localhost,Default port 25,Tencent (port 465 or 587)
smtpobj.set_debuglevel(1)
smtpobj.login(mail_user,mail_pass)#Log in to the QQ mailbox server
smtpobj.sendmail(sender,receivers,message.as_string())#send email
print("Mail sent successfully")
smtpobj.quit()#drop out
except smtplib.SMTPException as e :print("Error:Unable to send mail")print(e)
2、 Results of the:
3、 Encounter problems:
3.1、 Port error SMTP default port is 25, but QQ mailbox is not
smtplib.SMTP_SSL(mail_host,465)
To use SMTP_SSL
3.2、 Send connection refused 535, b'Error:
3.3、 Get authorization code:
The authorization code will be displayed here, write it down.
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts