1.邮箱开启smtp 获取到code
2.编写代码
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
##设置邮箱信息
# 发件邮箱服务器
host_server = "smtp.126.com" #这是网易126邮箱
#发送邮箱
sender = "****"
#接收邮箱
user = "****"
# 第一步获取到的code
code = "*****"
#邮箱标题
email_title = "这是邮箱标题"
#邮箱正文
email_content = "这是邮箱正文"
#创建附件实例
attachment = MIMEApplication(open('data.xlsx', 'rb').read())
#增加附件头文件
attachment.add_header('Content-Disposition', 'attachment', filename="data.xlsx")
#创建smtp实例
smtp = smtplib.SMTP(host_server)
#登录
smtp.login(sender,code)
#创建MIMEMultipart实例
msg = MIMEMultipart()
msg['Subject'] = email_title
msg['From'] = sender
msg['To'] = user
msg.attach(MIMEText(email_content))
msg.attach(attachment)
#发送邮件
smtp.sendmail(sender,user, msg.as_string())
网友评论