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

    使用python完成邮件的发送

    1、邮件需要的基本信息

    2、python发送邮件

    1、邮件需要的基本信息

    发件人的服务器、发送人、接收人(是否多个)、主题、内容,是否有附件

    2、python发送邮件

    import smtplib
    from email.mime.text import MIMEText  #此模块可以用于发送正文
    from email.mime.multipart import MIMEMultipart #此模块用于发送带附件的邮件


    #邮件基础信息配置
    smtpserver = "smtp.163.com" #发件服务器,qq邮箱为smtp.qq.com
    port = 0    #端口,qq邮箱为465
    sender = "XX@163.com" #发件箱
    psw = " XXXX"  #qq邮箱为授权码
    receiver = "XXXXXX@qq.com" #收件箱,多个邮箱为["XX@qq.com","XXXX@163.com"] 
    msg=MIMEMultipart()
    msg["from"]=sender
    msg["to"]=receiver  #发给单个邮箱
    #msg["to"]=";".join(receiver)  #发给多个邮箱
    msg["subject"]="test"

    body=MIMEText(mail_body,"html","utf-8")
    msg.attach(body)                               #读取附件中的正文作为邮件正文

    #附件信息的配置
    file_path=r"D:
    esult.html"  #读取本地的一个一个测试报告
    with open(file_path,"rb") as fp:
        mail_body=fp.read()
    
    
    att=MIMEText(mail_body,"basse64","utf-8")
    att["Content-Type"]="application/octet-stream"
    att["Content-Disposition"]='attachment; filename="test_report.html"'  #将系统中的result.html重命名一下作为附件发送
    msg.attach(att)

    #兼容一般邮箱和qq邮箱
    try:
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver)  #连接服务器 ,一般邮箱,如163等
        smtp.login(sender,psw)    #登录
    except:
        smtp=smtplib.SMTP_SSL(smtpserver,port) #连接服务器,qq邮箱
        smtp.login(sender,psw)    #登录
    smtp.sendmail(sender,receiver,msg.as_string()) #发送
    smtp.quit()    #关闭
     





  • 相关阅读:
    【vue】饿了么项目-goods商品列表页开发
    【vue】饿了么项目-header组件开发
    转转hybrid app web静态资源离线系统实践
    从列表到详情,没你想的那么简单
    浅谈Async/Await
    小程序代码包压缩 策略&方案
    微信小程序使用场景延伸:扫码登录、扫码支付
    这一次带你彻底了解Cookie
    常见函数错误引发的思考.
    触碰密码世界的大门
  • 原文地址:https://www.cnblogs.com/weizhideweilai/p/9170326.html
Copyright © 2011-2022 走看看