这篇教程python自动发送QQ邮箱的完整步骤写得很实用,希望能帮到您。
一、授权码获取
开启它: 
发送短信: 
发送后点击我已发送: 
把这个授权码复制下来保存起来,下次还可以用。
二、发送文本和附件你只需要修改邮箱,授权码,当然如果你想发送附件也把附件路径加上即可。 python代码: # coding=gbk"""作者:川川@时间 : 2021/11/10 10:50群:970353786"""import smtplibfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.mime.application import MIMEApplication# 写成了一个通用的函数接口,想直接用的话,把参数的注释去掉就好def send_email(msg_from, passwd, msg_to, text_content, file_path=None): msg = MIMEMultipart() subject = "python 实现邮箱发送邮件" # 主题 text = MIMEText(text_content) msg.attach(text) # file_path = r'read.md' #如果需要添加附件,就给定路径 if file_path: # 最开始的函数参数我默认设置了None ,想添加附件,自行更改一下就好 docFile = file_path docApart = MIMEApplication(open(docFile, 'rb').read()) docApart.add_header('Content-Disposition', 'attachment', filename=docFile) msg.attach(docApart) print('发送附件!') msg['Subject'] = subject msg['From'] = msg_from msg['To'] = msg_to try: s = smtplib.SMTP_SSL("smtp.qq.com", 465) s.login(msg_from, passwd) s.sendmail(msg_from, msg_to, msg.as_string()) print("发送成功") except smtplib.SMTPException as e: print("发送失败") finally: s.quit()msg_from = '283****79@qq.com' # 发送方邮箱passwd = 'd******a' # 填入发送方邮箱的授权码(就是刚刚你拿到的那个授权码)msg_to = '283******9@qq.com' # 收件人邮箱,我是自己发给自己text_content = "hi,this is a demo!" # 发送的邮件内容file_path = 'read.md' # 需要发送的附件目录send_email(msg_from,passwd,msg_to,text_content,file_path) 运行:(收到邮箱) 
三、继续升级你是否可以在这基础上再做改动,比如爬取某个网页的主要内容发送到邮箱?爬虫有趣的东西多着呢!比如我自动填体温,把填报后的效果发送给我邮箱。 python代码:(txt里面为我的具体内容) # coding=gbk"""作者:川川@时间 : 2021/11/10 11:50群:970353786"""import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.application import MIMEApplicationdef send_email(msg_from, passwd, msg_to, text_content): msg = MIMEMultipart() subject = "计算机自动填体温结果" # 主题 text = MIMEText(text_content) msg.attach(text) msg['Subject'] = subject msg['From'] = msg_from msg['To'] = msg_to try: s = smtplib.SMTP_SSL("smtp.qq.com", 465) s.login(msg_from, passwd) s.sendmail(msg_from, msg_to, msg.as_string()) print("发送成功") except smtplib.SMTPException as e: print("发送失败") finally: s.quit()msg_from = '28****579@qq.com' # 发送方邮箱passwd = 'dw****rodhda' # 填入发送方邮箱的授权码(就是刚刚你拿到的那个授权码)msg_to = '2****9579@qq.com' # 收件人邮箱with open("log_t.txt", "r",encoding="utf-8") as f: # 打开文件 data = f.read() # 读取文件 text_content = data # 发送的邮件内容 send_email(msg_from,passwd,msg_to,text_content) 运行效果: 
四、声明自动邮箱发送仅仅用于个人学习练习,若用于其它等用途,后果自负,概不负责。 到此这篇关于python自动发送QQ邮箱的文章就介绍到这了,更多相关python自动发送QQ邮箱内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net! TensorFlow人工智能学习数据填充复制实现示例 Python Django框架设计模式详解 |