zoukankan      html  css  js  c++  java
  • 发邮件 文字+ 附件的方法(QQ or 网易 邮箱)

    #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="#####@163.com"#发件的邮箱
    # psw="#####"#授权码
    # receiver=["######@qq.com"]#收件的邮箱

    # QQ邮箱用这个
    smtpserver="smtp.qq.com"#发件服务器
    port=465#端口
    sender="#####@qq.com"#发件的邮箱
    psw="######"#授权码
    receiver=["######@163.com"]#收件的邮箱

    # ---------- ---------- 2. 编辑邮 件的内容 ------------

    # 读取附件文件
    file_path = "D:/report.html" #附件地址
    with open(file_path, "rb") as fp:
      mail_body = fp.read()

    # 装载信息和内容
    msg = MIMEMultipart()
    msg["from"] = sender
    msg["to"]=";".join(receiver)

    msg["subject"] = "这个我的主题 "
    body="<p>这个是发送的163邮件</p>"

    # 指定展示正文内容,body改为mail_body即展示附件内容
    body = MIMEText(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.htm"'
    msg.attach(att)


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

  • 相关阅读:
    Insertion Sort List
    Max Points on a Line
    Evaluate Reverse Polish Notation
    vue路由传参的三种基本方式
    如何搭建一个vue项目
    vue路由跳转时更改页面title
    CSS清除浮动大全共8种方法
    border:none 与border:0的区别
    for..in和for..of的功能
    IE浏览器兼容问题-----html和css的兼容写法
  • 原文地址:https://www.cnblogs.com/kaibindirver/p/8218200.html
Copyright © 2011-2022 走看看