Python uses SMTP service to send mail

1.demo

The content marked (*) needs to be modified

# coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header


class Mail:
    def __init__(self):
        # 第三方 SMTP 服务

        self.mail_host = "smtp.qq.com"  # 设置QQ邮箱服务器
        self.mail_pass = "eymdiwqeqweqwqwe"  # 授权码(*)
        self.sender = '[email protected]'  # 你的邮箱地址(*)
        self.receivers = ['[email protected]']  # 收件人的邮箱地址(可多个)(*)

    def send(self, mail_content):

        content = mail_content  # 邮件内容
        message = MIMEText(content, 'plain', 'utf-8')

        message['From'] = '[email protected]'  # 发件人(*)
        message['To'] = 'Your Majesty'  # 收件人(*)
        message['Subject'] = 'are you hanpi?'  # 标题(*)

        try:
            smtpObj = smtplib.SMTP_SSL(self.mail_host, 465)
            smtpObj.login(self.sender, self.mail_pass)
            smtpObj.sendmail(self.sender, self.receivers, message.as_string())
            smtpObj.quit()
            print('邮件发送成功')
        except smtplib.SMTPException as e:
            print('邮件发送失败')


if __name__ == '__main__':
    mail = Mail()
    mail.send('i think you are')  # 邮件内容(*)

2. Authorization code

QQ mailbox->Settings->Account->POP3/SMTP service->Get authorization code

3. Effect

Guess you like

Origin blog.csdn.net/qq_41854291/article/details/109096805