Chapter 15 ■ IMap
271
hostname, username = sys.argv[1:]
c = IMAPClient(hostname, ssl=True)
try:
c.login(username, getpass.getpass())
except c.Error as e:
print('Could not log in:', e)
else:
print('Capabilities:', c.capabilities())
print('Listing mailboxes:')
data = c.list_folders()
for flags, delimiter, folder_name in data:
print(' %-30s%s %s' % (' '.join(flags), delimiter, folder_name))
finally:
c.logout()
if name == 'main':
main()
You can see immediately from the code that more details of the protocol exchange are now being handled on
your behalf. For example, you no longer get a status code back that you have to check every time you run a command;
instead, the library is doing that check for you and will raise an exception to stop you in your tracks if anything goes
wrong. Figure 15-1 provides a sample conversation between Python and an IMAP server.
Figure 15-1. An example conversation between Python and an IMAP server