美文网首页
使用python3发送163邮件

使用python3发送163邮件

作者: S_jie | 来源:发表于2021-12-20 11:11 被阅读0次

1.登录163邮箱开启SMTP服务 开启服务后复制登录密码和服务器地址

开启设置 服务器地址

2.构建python代码

    def send_mail():
        # 发送邮箱服务器
        smtp_server = smtp.163.com

        # 发送邮箱用户名密码
        user = '你的163邮箱名称'
        password = '开启设置的时候复制的登录密码'

        # 接收邮箱
        receives = ['你需要发给的邮箱1','你需要发给的邮箱2','你需要发给的邮箱3'...]

        # 发送邮件和主题内容
        subject = '异常警告...'
        content = '<html><h1 style="color:red">测试邮件发送</h1></html>'

        # 构建发送与接收信息
        msg_root = MIMEMultipart()
        msg_root.attach(MIMEText(content, 'html', 'utf-8'))
        msg_root['subject'] = subject
        msg_root['From'] = user
        msg_root['To'] = ','.join(receives)

        # 构造附件3(附件为HTML格式的网页)
        att3 = MIMEText(open('发送附件的地址', 'rb').read(), 'base64', 'utf-8')
        att3["Content-Type"] = 'application/octet-stream'
        att3["Content-Disposition"] = 'attachment; filename="report_test.html"'
        msg_root.attach(att3)

        # SSL协议端口号要使用465
        smtp = smtplib.SMTP_SSL(smtp_server, 465)

        # H E L O 向服务器标识用户身份
        smtp.helo(smtp_server)
        # 服务器返回结果确认
        smtp.ehlo(smtp_server)
        # 登录邮箱服务器用户名和密码
        smtp.login(user, password)

        logging.info("Start send email...")

        smtp.sendmail(user, receives, msg_root.as_string())

        smtp.quit()
        logging.info("Send End!")
        logging.info('============================================================================================')

相关文章

网友评论

      本文标题:使用python3发送163邮件

      本文链接:https://www.haomeiwen.com/subject/cqzafrtx.html