diff --git a/homeassistant/components/notify/smtp.py b/homeassistant/components/notify/smtp.py index 0530ac4072d..f4f0d4cf57b 100644 --- a/homeassistant/components/notify/smtp.py +++ b/homeassistant/components/notify/smtp.py @@ -140,15 +140,20 @@ class MailNotificationService(BaseNotificationService): self.username = username self.password = password self.recipient = recipient + self.tries = 2 + self.connect() + + + def connect(self): self.mail = smtplib.SMTP(self._server, self._port) self.mail.ehlo_or_helo_if_needed() if self.starttls == 1: self.mail.starttls() self.mail.ehlo() - self.mail.login(self.username, self.password) + def send_message(self, message="", **kwargs): """ Send a message to a user. """ @@ -160,4 +165,10 @@ class MailNotificationService(BaseNotificationService): msg['From'] = self._sender msg['X-Mailer'] = 'HomeAssistant' - self.mail.sendmail(self._sender, self.recipient, msg.as_string()) + for attempt in range(self.tries): + try: + self.mail.sendmail(self._sender, self.recipient, msg.as_string()) + break + except smtplib.SMTPException as e: + _LOGGER.warning('SMTP Exception sending mail: %s' % e) + self.connect() \ No newline at end of file