zoukankan      html  css  js  c++  java
  • Python 发送邮件

    # coding:utf-8
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart

    # ----------1.跟发件相关的参数------

    smtpserver = "smtp.163.com"           # 发件服务器
    port = 0                              # 端口
    sender = "yoyo@163.com"     # 账号
    psw = "*********"                  # 密码
    # receiver = ["xxxx@qq.com"]      # 单个接收人也可以是list
    receiver = ["xxxx@qq.com", "yoyo@qq.com"]   # 多个收件人list对象

    # ----------2.编辑邮件的内容------
    # 读文件
    file_path = "result.html"
    with open(file_path, "rb") as fp:
        mail_body = fp.read()

    msg = MIMEMultipart()
    msg["from"] = sender                       # 发件人
    msg["to"] = ";".join(receiver)             # 多个收件人list转str
    msg["subject"] = "这个我的主题999"              # 主题

    # 正文
    body = MIMEText(mail_body, "html", "utf-8")
    msg.attach(body)

    # 附件
    att = MIMEText(mail_body, "base64", "utf-8")
    att["Content-Type"] = "application/octet-stream"
    att["Content-Disposition"] = 'attachment; filename="test_report.html"'
    msg.attach(att)

    # ----------3.发送邮件------
    try:
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver)                      # 连服务器
        smtp.login(sender, psw)
    except:
        smtp = smtplib.SMTP_SSL(smtpserver, port)
        smtp.login(sender, psw)                       # 登录
    smtp.sendmail(sender, receiver, msg.as_string())  # 发送
    smtp.quit()                                       # 关闭

  • 相关阅读:
    HDU 4686
    二叉索引树——树状数组
    poj 3548 Restoring the digits(DFS)
    poj 2062 Card Game Cheater(排序+模拟)
    poj 2570 Fiber Network(floyd)
    hdu 1080 Human Gene Functions
    hdu 4512 吉哥系列故事——完美队形I(最长公共上升自序加强版)
    2015 Multi-University Training Contest 2
    poj 1258 Agri-Net(最小生成树)
    2015 Multi-University Training Contest 1记录
  • 原文地址:https://www.cnblogs.com/tangqiu/p/9187629.html
Copyright © 2011-2022 走看看