python - 使用 imaplib 阅读电子邮件 - "Got more than 10000 bytes"错误

标签 python gmail imaplib

我正在尝试使用 imaplib 连接到我的 gmail 帐户:

import imaplib
mail = imaplib.IMAP4_SSH('imap.gmail.com')
mail.login('myemail@gmail.com', 'mypassword')
mail.select("inbox")
# returns ('OK', [b'12009'])

这一切似乎工作得很好,但是:

mail.search(None, "ALL")
# returns error: command: SEARCH => got more than 10000 bytes
mail.logout()
# returns ('NO',
# ["<class 'imaplib.IMAP4.error'>: command: LOGOUT => got more than 10000 bytes"])

我尝试访问的帐户的收件箱中有大约 9,000 封电子邮件。我用另一个少于 1,000 的帐户尝试了上述操作,代码工作正常。

第一个电子邮件帐户的问题是否与其中的邮件数量有关?是否有一些默认设置实现了一些大小限制?

如何绕过错误并阅读我的电子邮件?

最佳答案

Is the issue with the first email account related to the number of mails in it?

不是直接,但是是的,差不多。问题在于您正尝试一次下载包含 9000 条消息的整个列表。

发送长得可笑的行是一种有用的 DoS 攻击,对于用 C 而不是 Python 实现的程序,缓冲区溢出攻击对许多网络客户端和服务器。它也可能非常慢,并阻塞网络。但请注意,RFC 最后一次更新是在 1999 年,而 imaplib 是在 1997 年编写的,因此“荒谬”的限制可能从那时起已经改变。

根据 RFC 2683 解决此问题的正确方法,就是不要尝试那样做。 (具体参见第 3.2.1.5 节。)


Is there some default setting that implements some size limit?

是的。它没有在文档中列出,但由于 RFC 建议限制为 8000 字节,而它允许 10000,我想这是合理的。


How can I get around the error and read my emails?

同样,您应该做的是将其分解为更小的读取。

但只要 gmail 对这么大的搜索没有问题,并且您很乐意要求计算机和网络连接比 90 年代后期的标准好一点,您就可以绕过这个问题。

幸运的是,与 stdlib 中的许多模块一样,imaplib 被编写为有用的示例代码,同时也被用作一个模块。你总能看出是这种情况,因为 the documentation链接到 the source就在顶部。

所以,如果你看一看,你会看到,离顶部不远:

# reading arbitrary length lines. RFC 3501 and 2060 (IMAP 4rev1)
# don't specify a line length. RFC 2683 however suggests limiting client
# command lines to 1000 octets and server command lines to 8000 octets.
# We have selected 10000 for some extra margin and since that is supposedly
# also what UW and Panda IMAP does.
_MAXLINE = 10000

所以,如果你想覆盖它,你可以 fork 模块(将 imaplib.py 保存为 myimaplib.py 并改用它),或者你可以只是monkeypatch 在运行时:

import imaplib
imaplib._MAXLINE = 40000

当然,您必须选择一个您认为更能反射(reflect) 2014 年荒谬边缘的数字。

关于python - 使用 imaplib 阅读电子邮件 - "Got more than 10000 bytes"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25457441/

相关文章:

python - 如何给定时器线程一个名字?

php - 尝试使用 gmail 发送电子邮件时出现 Cakephp 3 错误

java - Gmail 作为 JavaMail SMTP 服务器

ruby-on-rails - Rails 和 Gmail SMTP,如何使用自定义发件人地址

python - 如何使用 IMAPClient 将电子邮件标志更改为最近

Python - 在文件中查找特定字符串

python - python 3.3.2+ 中的 yield from 和 yield 之间有什么区别

python - 你安装了mysqlclient吗?

python - imaplib - 在 PC 上工作但在服务器上不起作用?

python - 使用 imaplib 在 gmail 中删除电子邮件时出现问题