1. Technology

Retrieving POP3 Email with poplib


5 of 5

Downloading the Messages
To download the messages, we first need to retrieve how many there are. To do this, we use Python's built-in len() function in conjunction with the list() method of the Mailbox class.
 messageCount = len(Mailbox.list()[1]) 
Then we simply set up a loop that counts the messages and retrieves each message in sequence until the end of the box is reached.
 for i in range(messageCount): 
 for j in Mailbox.retr(i+1)[1]: 
 print j 
This merely prints the messages to the screen. Naturally, if you have more than a couple messages on the server, you will not get to see them all in one go. It is best, therefore, to either pipe the output to a text file or rewrite the code to write it to a file, perhaps named after the server address input by the user.
  1. About.com
  2. Technology
  3. Python
  4. Web Development
  5. Python Programming - Retrieving Email - Pop POP3 with poplib - Downloading the Messages

©2014 About.com. All rights reserved.