#!/usr/bin/python3 # -*- coding=utf-8 -*- import smtplib import time from email.mime.text import MIMEText from email.header import Header from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication #收件人和发件人 receiver = 'xxx' sender = 'xxx' #发件人邮箱的SMTP服务器(即sender的SMTP服务器) smtpserver = 'xxx' #发件人邮箱的用户名和授权码(不是登陆邮箱的密码) username = 'xxx' password = 'xxx' #(xxxx@xx.com邮箱的授权码) mail_title = 'shift plan' mail_body = 'please check the attachment' #创建一个实例 message = MIMEMultipart() #邮件正文 # (plain表示mail_body的内容直接显示,也可以用text,则mail_body的内容在正文中以文本的形式显示,需要下载) # 添加附件,传送文件 part = MIMEApplication(open('C:/Users/Administrator/Desktop/1.txt','rb').read()) part.add_header('Content-Disposition', 'attachment', filename="1.txt") message.attach(part) body=(''' this is a test <br>/n</br> test2 ''') # message ['From'] = sender #邮件上显示的发件人 message['To'] = receiver #邮件上显示的收件人 message['Subject'] = Header( mail_title, 'utf-8' ) #邮件主题 message.attach(MIMEText(body, 'html', 'utf-8')) smtp = smtplib.SMTP() #创建一个连接 time.sleep(5) smtp.connect( smtpserver ) #连接发送邮件的服务器 smtp.login( username, password ) #登录服务器 smtp.sendmail( sender, receiver, message.as_string() ) #填入邮件的相关信息并发送 smtp.quit()