# -*- 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 = 'chenuujia@163.com'
#接收者
receiver = 'hang@bit.edu.cn'
smtpserver = 'smtp.163.com'
username = 'cheuujia'
password = 'chen985'
mail_title = 'SmartClass_Result'
# 发送者
sender = '3220111@bit.edu.cn'
# 接收者
# receiver = 'hang@bit.edu.cn'#,12561896@qq.com,1476992@qq.com'
# receiver='mr_ifan@163.com,1476992@qq.com'
receiver='haing@bit.edu.cn,320814@bit.edu.cn,3220874@bit.edu.cn,322966@bit.edu.cn,wczw@bit.edu.cn'
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邮件发送
最新推荐文章于 2023-07-28 14:44:27 发布