• python学习(2)--发邮件带附件


    # -*- coding:utf-8 -*-
    from email.mime.text import MIMEText
    from email.header import Header
    import smtplib
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart
    
    
    msg = MIMEMultipart() 
    msg['Subject'] = Header("来自Python的邮件",'utf-8')
    msg['From'] = Header('***@sina.com')
    msg['To'] = Header('receiver','utf-8')
    
    from_addr = '***@sina.com' #发件邮箱
    password = '****' #邮箱密码
    to_addr = '****@163.com' #收件邮箱
    
    smtp_server = 'smtp.sina.com' #SMTP服务器
    
    part = MIMEApplication(open('result.jtl','rb').read()) 
    part.add_header('Content-Disposition', 'attachment', filename="result.jtl") 
    msg.attach(part)
    
    
    try:
        server = smtplib.SMTP(smtp_server,25) #第二个参数为默认端口为25,有些邮件有特殊端口
        print('开始登录')
        server.login(from_addr,password) #登录邮箱
        print('登录成功')
        print("邮件开始发送")
        server.sendmail(from_addr,to_addr,msg.as_string())  #将msg转化成string发出
        server.quit()
        print("邮件发送成功")
    except smtplib.SMTPException as e:
        print("邮件发送失败",e)
    

      

  • 相关阅读:
    佳佳的 Fibonacci
    毒瘤之神的考验
    An error occurred while searching for implementations of method
    eclipse 开发 scala
    hbase的数据模型
    Hbase和RDBMS(关系数据库管理系统)区别
    hbase和mapreduce开发 WordCount
    使用eclipse开发hbase程序
    hbase 的体系结构
    hbase 遇到过的问题
  • 原文地址:https://www.cnblogs.com/yetj/p/9030078.html
Copyright © 2020-2023  润新知