2
我使用以下代码从Gmail下载我的所有电子邮件,但不幸的是,返回的电子邮件总数与帐户中的电子邮件总数不匹配。特别是,我能够获得前43条消息,但是我在收件箱中错过了20多个。也许这是对可以退回的数字的某种限制(?)。预先感谢您提供的任何帮助!通过python从gmail获取电子邮件
import imaplib, email, base64
def fetch_messages(username, password):
messages = []
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login(username, password)
conn.select()
typ, data = conn.uid('search', None, 'ALL')
for num in data[0].split():
typ, msg_data = conn.uid('fetch', num, '(RFC822)')
for response_part in msg_data:
if isinstance(response_part, tuple):
messages.append(email.message_from_string(response_part[1]))
typ, response = conn.store(num, '+FLAGS', r'(\Seen)')
return messages
您是否试过conn.search(无,'ALL')获取所有消息? – theodox 2013-02-25 21:00:00
是的,我更新了代码片段以明确'status'设置为'ALL' – vgoklani 2013-02-25 21:03:44
您可以尝试使用conn.search()而不是conn.uid()。你会得到索引而不是uid,但是当你获取消息时你可以得到uid。 – theodox 2013-02-25 21:15:24