【问题标题】:Why does FETCH command error: BAD [b'Could not parse command'] fail occur?为什么会出现 FETCH command error: BAD [b'Could not parse command'] 失败?
【发布时间】:2020-06-07 06:08:16
【问题描述】:
import imaplib
import email
import socket

socket.getaddrinfo('127.0.0.1', 8080)


def read_email_from_gmail():
    try:
        mail = imaplib.IMAP4_SSL('imap.gmail.com', 993)
        mail.login('your email', 'your password')
        mail.select('inbox')

        type, data = mail.search(None, 'ALL')
        mail_ids = data[0]

        id_list = mail_ids.split()

        typ, data = mail.fetch(bytes(str(id_list), 'utf-8'), '(RFC822)')

        for responsepart in data:
            if isinstance(responsepart, tuple):
                msg = email.message_from_string(responsepart[1].decode('utf-8'))
                email_subject = msg['subject']
                email_from = msg['from']
                print('From : ' + email_from + '\n')
                print('Subject : ' + email_subject + '\n')

    except Exception as e:
        print(str(e))


read_email_from_gmail()


输出:FETCH 命令错误:BAD [b'Could not parse command']

我想使用 Python 接收来自 Gmail 的邮件。有人可以帮我在哪里出错吗?这是什么错误?

提前谢谢你!

【问题讨论】:

  • 请不要在发布后更改您的问题。另外,请尝试不使用 imaplib 调试和跟踪工具。此外,通过在 id_list 上使用 for 循环,从一次获取 一个 消息开始,而不是尝试一次获取所有消息。 fetch() 不接受 python 列表,只是将其强制为“字节”并不能使其为服务器所接受。

标签: python imap getaddrinfo


【解决方案1】:

我认为您的服务器名称错误。应该是imap.gmail.com

这就是我的想法。我搜索了 11001 和 getaddrinfo,得知此错误代码来自 Windows,表示找不到主机。

我尝试 ping imap.google.com,但无法解析主机名。

我搜索了“google imap server”,在Google's documentation 中发现了一个不同的主机名。

【讨论】:

  • 谢谢!我得到了它!但是,一旦我解决了你告诉我的错误,我的输出中就没有任何内容了。你能看出我哪里出错了吗?
  • 我不知道是不是这个问题,但是您使用的是 SMTP 服务器的端口号。
  • 当您更改问题的标题并修复其中的代码时,它对未来遇到相同问题的读者的用处会降低。我建议您恢复原始代码和标题,并将您当前的问题作为单独的问题发布。无论如何,这不是我可以轻易回答的问题。
猜你喜欢
  • 2021-10-11
  • 2018-09-21
  • 2011-04-11
  • 2017-09-03
  • 1970-01-01
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多