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

    # coding=utf-8
    import sys
    import smtplib
    import os
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    
    from ConfigHelper import ConfigHelper
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    
    class SendEmail():
    
        @staticmethod
        def Send(configPath, content, attPath):
            f = ConfigHelper.Get(
                configPath, 'email', 'from')
            pwd = ConfigHelper.Get(
                configPath, 'email', 'pwd')
            t = ConfigHelper.Get(
                configPath, 'email', 'to')
            message = MIMEMultipart()
    # 出现554 是因为from  to没有设置
            message['From'] = f
            message['To'] = t
            message['Subject'] = '邮件通知'
            message.attach(MIMEText(content, 'plain', 'utf-8'))
            # 构造附件
            if os.path.exists(attPath):
                att1 = MIMEText(open(attPath, 'rb').read(), 'base64', 'utf-8')
                att1["Content-Type"] = 'application/octet-stream'
                att1[
                    "Content-Disposition"] = 'attachment; filename="%s"' % os.path.split(attPath)[1]
                message.attach(att1)
    
            server = smtplib.SMTP('smtp.163.com', 25)
            server.login(f, pwd)
            server.sendmail(f, t, message.as_string())
            server.quit()
  • 相关阅读:
    DS博客作业04--图
    DS博客作业03--树
    DS博客作业02--栈和队列
    DS01-线性表
    c博客06-结构体&文件
    C博客作业05--指针
    123
    面向对象设计大作业
    购物车
    有理数类的设计
  • 原文地址:https://www.cnblogs.com/zhaoyihao/p/6655128.html
Copyright © 2011-2022 走看看