解决方法:
发送的消息体加入键值对:
"From":"xxxx.com"
"To":"xxxx.com"
from smtplib import SMTP
from email.header import Header
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import urllib
def main():
message = MIMEMultipart()
text_content = MIMEText('附件中带有数据,请查收', 'plain', 'utf-8')
message['Subject'] = Header('本月数据', 'utf-8')
message['From'] = 'xxxx@126.com'
message['To'] = 'xxxx@qq.com'
message.attach(text_content)
with open('email_.txt', 'rb') as f:
txt = MIMEText(f.read(), 'base64', 'utf-8')
txt['Content-Type'] = 'text/plain'
txt['Content-Disposition'] = 'attachment; filename=email_.txt'
message.attach(txt)
with open('email_.xlsx', 'rb') as xf:
xls = MIMEText(xf.read(), 'base64', 'utf-8')
xls['Content-Type'] = 'application/vnd.ms-excel'
xls['Content-Disposition'] = 'attachment; filename=email_.xlsx'
message.attach(xls)
smtper = SMTP('smtp.126.com')
smtper.starttls()
sender = 'xxxx@126.com'
receiver = ['xxxx@qq.com']
# xxxx: 相应邮箱的客户端授权码
smtper.login(sender, 'xxxx')
smtper.sendmail(sender, receiver, message.as_string())
smtper.quit()
print('发送成功!')
if __name__ == '__main__':
main()