python -- 通过QQ邮箱发送邮件

如何使用python通过qq邮箱给其他人的邮箱发送邮件呢

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 26 11:23:58 2023

@author: jianpu

@blog :  https://blog.csdn.net/weixin_44237337?spm=1000.2115.3001.5343

@email: [email protected]

introduction : keep learning althongh walk slowly
"""






import logging
from concurrent.futures import ThreadPoolExecutor
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL


# qq邮箱smtp服务器
host_server = 'smtp.qq.com'
# sender_qq为发件人的qq号码
sender_qq = '**************'
# pwd为qq邮箱的授权码
pwd = '**************'
# 发件人的邮箱
sender_qq_mail = '*******@qq.com'
# 邮件标题
mail_title = '来自linux 子系统 的邮件'
# 邮件的正文内容
mail_content = '你好,我是来自简朴子系统-jianpu ,现在在进行一项用python登录qq邮箱发邮件的测试'
# 收件人邮箱列表
receiver_list = ['*****@163.com', '*****@qq.com', '*****@qq.com','*****@qq.com']


def send_email(receiver):
    try:
        # ssl登录
        smtp = SMTP_SSL(host_server)
        smtp.ehlo(host_server)
        smtp.login(sender_qq, pwd)

        msg = MIMEText(mail_content, "plain", 'utf-8')
        msg["Subject"] = Header(mail_title, 'utf-8')
        msg["From"] = sender_qq_mail
        msg["To"] = receiver

        smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
        smtp.quit()

        logging.info(f"邮件发送成功,收件人:{receiver}")
    except Exception as e:
        logging.error(f"邮件发送失败,收件人:{receiver},错误信息:{e}")


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
    with ThreadPoolExecutor(max_workers=10) as executor:
        for receiver in receiver_list:
            executor.submit(send_email, receiver)

上述代码中有一个比较重要的东西就是邮箱的授权码,详情获取方式如下:
https://service.mail.qq.com/detail/0/75
python -- 通过QQ邮箱发送邮件_第1张图片
进入qq邮箱-账号-账号安全-POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,开启服务即可:
python -- 通过QQ邮箱发送邮件_第2张图片
其中,如果已经开启了,点击管理服务,会进入如下页面,就可以获得授权码了:

python -- 通过QQ邮箱发送邮件_第3张图片

  • 正常情况下,输入发送人的邮箱以及接收人的邮箱,就可以发送了,那么如何实现定时发送邮箱呢?
    在linux系统中,可以通过shell脚本结合cronb

你可能感兴趣的:(一些奇怪的知识,如何解决系列,python,开发语言)