Improve IMAP server connection error handling
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 5 Jul 2012 09:48:28 +0000 (11:48 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 5 Jul 2012 09:48:28 +0000 (11:48 +0200)
- Don't crash on wrong DNS names
- Give a more specific description on initial connection errors

src/mail_iterator.py

index 2220a3e..deae0bd 100644 (file)
@@ -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."""