这是对我有用的代码-使用python发送带有附件的电子邮件
#!/usr/bin/pythonimport smtplib,sslfrom email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEbasefrom email.mime.text import MIMETextfrom email.utils import formatdatefrom email import enprersdef send_mail(send_from,send_to,subject,text,files,server,port,username='',password='',isTls=True): msg = MIMEMultipart() msg['From'] = send_from msg['To'] = send_to msg['Date'] = formatdate(localtime = True) msg['Subject'] = subject msg.attach(MIMEText(text)) part = MIMEbase('application', "octet-stream") part.set_payload(open("WorkBook3.xlsx", "rb").read()) enprers.enpre_base64(part) part.add_header('Content-Disposition', 'attachment; filename="WorkBook3.xlsx"') msg.attach(part) #context = ssl.SSLContext(ssl.PROTOCOL_SSLv3) #SSL connection only working on Python 3+ smtp = smtplib.SMTP(server, port) if isTls: smtp.starttls() smtp.login(username,password) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.quit()


