Max Li
Max Li

Reputation: 5219

Running the script for the 2-nd time, the messages are not retrieved from the mail server

I read the mails from my gmail account with the code following below.

import poplib
pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('user')             # result: '+OK send PASS'
pop_conn.pass_('password')        # result: '+OK Welcome.'
print pop_conn.list()[1]
pop_conn.quit()

It shows me 1 message as expected.
However, if I run this script for the second time, I get 0 messages as result. On the server the message is still there and unread.
How can I get all the messages also running the script for the second time?

For me it behaves as an email client that doesn't download the same mail twice. Is there some flag to force the program to download everything again? I use python 2.7.x on ubuntu 12.10

Upvotes: 1

Views: 169

Answers (1)

Aleksi Torhamo
Aleksi Torhamo

Reputation: 6632

Are you sure that's all that you were doing when it happened? As far as I know, just using list() shouldn't do that, but if you used retr() to read the message contents too, then POP3 servers often mark the message and won't return it on further connections anymore.

You could try using IMAP instead, since it lets you specify the behavior in more detail, eg. you can tell it to not mark messages as seen and allows you to retrieve them more than once. Google supports IMAP and python has imaplib for using it.

Upvotes: 1

Related Questions