try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username,password)
smtp.sendmail(sender,receiver,message.as_string())
except smtplib.SMTPException as e:
self.log.error("邮件发送失败" + str(e))
else:
self.log.info("邮件发送成功")
finally:
smtp.quit()
Jenkins构建python自动化的最后一步邮件发送
邮件发送失败报错:please run connect() first
查看日志如下
smtp报错:please run connect() first,根据报错信息看应该是连接SMTP服务问题于是查看Jenkins构建时控制台输出信息如下:
在邮件connect时报错:OSError: [Errno 101] Network is unreachable
经过多番查找筛选错误以及试错后,得知centOS系统上发送SMTP邮件需要用SMTP_SSL 和ehlo
更改代码如下:
try:
smtp = smtplib.SMTP_SSL(smtpserver)
smtp.ehlo(smtpserver)
smtp.login(username,password)
smtp.sendmail(sender,receiver,message.as_string())
except smtplib.SMTPException as e:
self.log.error("邮件发送失败" + str(e))
else:
self.log.info("邮件发送成功")
finally:
smtp.quit()