python自动发邮件

一、发送邮箱配置(以163邮箱为例)

设置步骤参考:https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2a5feb28b66796d3b

二、python实现自动发邮件

python发邮件需要两个模块:smtplib和email

  • smtplib模块主要负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容)。
  • email模块主要负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。
bash 复制代码
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage


class EmailManage:
    def send_email(self):
        # 发送邮件的用户名和客户端密码
        smtpserver = 'smtp.163.com'
        sender = '[email protected]'
        # 第三方客户端登录授权密码
        password = 'XPLTKDYUZFCUAZGC'
        #接收邮件的邮箱
        receiver = '[email protected]'
        # 邮件的主题
        subject = "全部类型"
        #邮件类型为"multipart/alternative"的邮件包括纯文本正文(text/plain)和超文本正文(text/html)。
        #邮件类型为"multipart/related"的邮件正文中包括图片,声音等内嵌资源。
        #邮件类型为"multipart/mixed"的邮件包含附件。向上兼容,如果一个邮件有纯文本正文,超文本正文,内嵌资源,附件,则选择mixed类型。
        message = MIMEMultipart('mixed')

        # 文字内容
        text = "你好\n文字内容测试\n访问路径:\nhttp://www.baidu.com"
        text_plain = MIMEText(text, 'plain', 'utf-8')
        message.attach(text_plain)

        # html内容
        html = """\
        <html>
          <body>
            <p>This is an example email body.</p>
            <p>It can be in HTML or plain text.</p>
          </body>
        </html>
        """
        text_html = MIMEText(html, 'html', 'utf-8')
        text_html["Content-Disposition"] = 'attachment; filename="texthtml.html"'
        message.attach(text_html)

        #图片内容
        sendimagefile = open(r'D:\python\file\1.jpg', 'rb').read()
        image = MIMEImage(sendimagefile)
        image.add_header('Content-ID', '<image1>')
        image["Content-Disposition"] = 'attachment; filename="1.png"'
        message.attach(image)

        # 附件
        sendfile = open(r'D:\python\file\测试.txt', 'rb').read()
        text_att = MIMEText(sendfile, 'base64', 'utf-8')
        text_att["Content-Type"] = 'application/octet-stream'
        text_att["Content-Disposition"] = 'attachment; filename="ceshi.txt"'
        message.attach(text_att)

        message['form'] = sender
        message['to']= receiver
        message['subject'] = subject

        #登录smtp服务器
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver)
        smtp.login(sender,password)
        #发送邮件
        smtp.sendmail(sender,receiver,message.as_string())
        #关闭连接
        smtp.quit()

if __name__ == '__main__':
    EmailManage().send_email()
相关推荐
小生凡一2 分钟前
腾讯二面:TCC分布式事务 | 图解TCC|用Go语言实现一个TCC
开发语言·分布式·golang
minji...6 分钟前
C语言 函数递归
c语言·开发语言·算法
梓羽玩Python14 分钟前
开源AI代理爆火!Suna:3天内新增5.5K+标星,自然对话驱动的自动化神器!
人工智能·python·github
咖啡调调。19 分钟前
模板引擎语法-过滤器
python·django·sqlite
云上空26 分钟前
C#初级知识总结
开发语言·c#
Ankie Wan30 分钟前
notepad++技巧:查找和替换:扩展 or 正则表达式
python·正则表达式·notepad++
带娃的IT创业者30 分钟前
《AI大模型趣味实战》智能Agent和MCP协议的应用实例:搭建一个能阅读DOC文件并实时显示润色改写过程的Python Flask应用
人工智能·python·flask
JavaEdge在掘金37 分钟前
启动nginx报错,80 failed (97: Address family not supported by protocol)
python
纪元A梦44 分钟前
华为OD机试真题——绘图机器(2025A卷:100分)Java/python/JavaScript/C++/C/GO最佳实现
java·javascript·c++·python·华为od·go·华为od机试题
钢铁男儿1 小时前
C# 深入理解类:面向对象编程的核心数据结构
开发语言·数据结构·c#