python 发送邮件,报错 raise SMTPServerDisconnected('please run connect() first')
SMTPServerDisconnected: please run connect() first
用邮箱服务器地址报错。
由于邮箱服务器发送文件没有设置账号密码 所以没写,请问哪里有问题呢?
msg = MIMEMultipart()
#邮件正文
msg.attach(MIMEText(mail_body, 'plain', 'utf-8'))
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = username
msg['To'] = receiver #
#附件:附件名称用英文
#for file_name in file_names:
att = MIMEText(open(file_name, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att['Content-Disposition'] = 'attachment;filename="%s"' % (file_name)
msg.attach(att)
# 服务器准备,登录并发送邮件
try:
#smtp = smtplib.SMTP()
#smtp = smtplib.SMTP_SSL(mail_host,465)
smtp = smtplib.SMTP()
smtp.connect('10.188.128.188')
#smtp.login(username, password)
smtp.sendmail(sender, receiver.split(','), msg.as_string())
except Exception as e:
print(e)
print("邮件发送失败!")
else:
print("邮件发送成功!")
finally:
smtp.quit()
print(__name__)
if __name__ == '__main__':
subject="Alarm Report"
mail_body="测试本文"
receiver="linlin@tri.com,106476769@qq.com" #"邮箱1,邮箱2" 接收人邮件地址 sep= ","
file_name='PPT_SendMail.py'
sendEmail(subject, mail_body, receiver,file_name)#file_names