gpt4 book ai didi

python - 如何从 IMAP 服务器获取最后 10 条消息?

转载 作者:行者123 更新时间:2023-11-28 21:50:54 29 4
gpt4 key购买 nike

我使用 imaplib2 库通过这样的命令搜索最后 10 条消息:

imap_client.search(None, '{}:{}'.format(last_uid, last_uid - 9))

但要获得 last_uid 我每次都需要执行这样的命令:

imap_client.select("INBOX", readonly=True)

获取最后一个 UID。

有什么方法可以:

  1. 在没有 select() 命令的情况下获取最后的 UID 获取最后 10 条消息
  2. 没有最后的 UID。可能有任何搜索条件,例如“LAST”或“-10:”?

我无法执行这样的命令 client.search(None, 'ALL'),因为 IMAP 服务器有超过 50K 条消息。

最佳答案

对于任何寻求此问题答案的 future 旅行者,我根据@arnt 给出的提示想出了代码。

svr = imaplib.IMAP4_SSL(server)
if svr.login(user=user, password=password):
print('User ' + user + ' logged in successfully.')
else:
print('Login for the user ' + user + " was denied. Please check your credentials.")

x = svr.select('inbox', readonly=True)
num = x[1][0].decode('utf-8')
#from here you can start a loop of how many mails you want, if 10, then num-9 to num
resp, lst = svr.fetch(num, '(RFC822)')
body = lst[0][1]
email_message = email.message_from_bytes(body)

这对我来说非常方便,因为我正在访问一封包含超过 67000 封电子邮件的电子邮件。

关于python - 如何从 IMAP 服务器获取最后 10 条消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31218582/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com