加载中...

Python

  1. Home
  2. Computing & Technology
  3. Python

Retrieving POP3 Email with poplib

From Al Lukaszewski, for About.com

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.

5 of 5

Explore Python

About.com Special Features

Python

  1. Home
  2. Computing & Technology
  3. Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.