Home >Backend Development >Python Tutorial >Python uses smtplib to implement QQ mailbox sending emails
This article mainly introduces python to use smtplib to implement QQ mailbox sending emails in detail. It has certain reference value. Interested friends can refer to it.
python's smtplib provides a very Convenient way to send emails. It simply encapsulates the SMTP protocol.
The following is an example of using smtplib to send emails through QQ mailbox.
First of all, you must open the smtp service of QQ mailbox, and open it in Settings-Account on the QQ mailbox personal homepage. As shown in the figure:
After successfully opening, click Generate Authorization Code, and the password obtained is the login password used by smtp when sending emails.
A simple example of sending an email:
from smtplib import SMTP_SSL from email.mime.text import MIMEText from email.header import Header email_from = "123456@qq.com" #改为自己的发送邮箱 email_to = "654321@qq.com" #接收邮箱 hostname = "smtp.qq.com" #不变,QQ邮箱的smtp服务器地址 login = "123456@qq.com" #发送邮箱的用户名 password = "xddflpwqesfkbidf" #发送邮箱的密码,即开启smtp服务得到的授权码。注:不是QQ密码。 subject = "python+smtp" #邮件主题 text = "send email" #邮件正文内容 smtp = SMTP_SSL(hostname)#SMTP_SSL默认使用465端口 smtp.login(login, password) msg = MIMEText(text, "plain", "utf-8") msg["Subject"] = Header(subject, "utf-8") msg["from"] = email_from msg["to"] = email_to smtp.sendmail(email_from, email_to, msg.as_string()) smtp.quit()
Script execution result:
PS: If you encounter an error in the SMTP_SSL statement (UnicodeDecodeError: 'utf-8' codec can't decode), it may be because the computer name contains Chinese characters.
Related recommendations:
Example of Python using openpyxl library to traverse Sheet
The above is the detailed content of Python uses smtplib to implement QQ mailbox sending emails. For more information, please follow other related articles on the PHP Chinese website!