From 94a6e9e7e9a8b616eaab7537877b6851ebd4b7d4 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Thu, 5 Jul 2012 11:48:28 +0200 Subject: [PATCH] Improve IMAP server connection error handling - Don't crash on wrong DNS names - Give a more specific description on initial connection errors --- src/mail_iterator.py | 17 +++++++++++++---- 1 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/mail_iterator.py b/src/mail_iterator.py index 2220a3e..deae0bd 100644 --- a/src/mail_iterator.py +++ b/src/mail_iterator.py @@ -40,19 +40,28 @@ class MailIterator: def __init__(self, server, username, password, skip_shared_folders = False): """Creates a connection and a user session.""" + self.skip_shared_folders = skip_shared_folders + + # connect to server try: self.mail_con = imaplib.IMAP4_SSL(server) + except Exception as ex: + raise UserWarning("Could not connect to host %s: %s" % (server, ex)) + + # log in + try: self.mail_con.login(username, password) logging.info("Logged in as %s.", username) - except (self.mail_con.error): + except: self.logged_in = False - raise UserWarning("Could not log in as user " + username + ".") + raise UserWarning("Could not log in as user " + username) self.logged_in = True + + # list mailboxes try: _result, self.mailboxes = self.mail_con.list() except (self.mail_con.error): - raise UserWarning("Could not retrieve mailboxes for user " + username + ".") - self.skip_shared_folders = skip_shared_folders + raise UserWarning("Could not retrieve mailboxes for user " + username) def __del__(self): """Closes the connection and the user session.""" -- 1.7.1