python邮件发送

# -*- coding:utf-8 -*-
# Code by Fan.Bai

import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

from email.mime.application import MIMEApplication
def sendmail(csvpath):
    #发送者
    sender = '[email protected]'
    #接收者
    receiver = '[email protected]'
    smtpserver = 'smtp.163.com'
    username = 'cheuujia'
    password = 'chen985'
    mail_title = 'SmartClass_Result'

    # 发送者
    sender = '[email protected]'
    # 接收者
    # receiver = '[email protected]'#,[email protected],[email protected]'
    # receiver='[email protected],[email protected]'
    receiver='[email protected],[email protected],[email protected],[email protected],[email protected]'
    smtpserver = 'smtp.bit.edu.cn'
    username = '3220111'
    password = 'zY544979'
    mail_title = 'SmartClass_Result'


    # 创建一个带附件的实例
    message = MIMEMultipart()
    message['From'] = sender
    message['To'] = receiver
    message['Subject'] = Header(mail_title, 'utf-8')

    #csvFile = './class_room_2019_fall.csv'
    csvFile=csvpath
    csvApart = MIMEApplication(open(csvFile, 'rb').read())
    csvApart.add_header('Content-Disposition', 'attachment', filename=csvFile)


    # 邮件正文内容
    message.attach(MIMEText('SmartClass_Result', 'plain', 'utf-8'))

    # 构造附件1(附件为TXT格式的文本)
    # att1 = MIMEText(open('./2082019-05-28_11_30_00_1.mp4.txt', 'rb').read(), 'base64', 'utf-8')
    # att1["Content-Type"] = 'application/octet-stream'
    # att1["Content-Disposition"] = 'attachment; filename="text1.txt"'
    message.attach(csvApart)

    smtpObj = smtplib.SMTP_SSL(host=smtpserver)  # 注意:如果遇到发送失败的情况(提示远程主机拒接连接),这里要使用SMTP_SSL方法
    smtpObj.connect(host=smtpserver)
    smtpObj.login(username, password)
    smtpObj.sendmail(sender, receiver.split(','), message.as_string())
    print("邮件发送成功!!!")
    smtpObj.quit()

if __name__ == '__main__':
    sendmail()

你可能感兴趣的:(python编程)