伊马利布。IMAP4.error:命令 SEARCH 在 AUTH 状态中是非法的,只有在 SELECTED 状态中才允许,即使我应该不在 auth 状态中

imaplib.IMAP4.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED even though I should be out of auth state

提问人:relu 提问时间:8/20/2023 更新时间:8/20/2023 访问量:49

问:

我收到此错误,我不知道为什么,这是方法:

def recieve(self, host: str, port: int, user: str, password: str, mailbox: str, batch_size: int = 0, logout_after: bool = True):
        """
        mailbox gotten from mailboxes() method.
        a batch size of 0 means all emails, batch sizes get most recent amount of emails in mailbox.
        """
        try:
            with imaplib.IMAP4_SSL(host, port) as imap:
                
                imap.login(user=user, password=password)
                imap.select(mailbox)
                rv, data = imap.search(None, "ALL")
                
                if batch_size == 0:
                    for num in data[0].split():
                        rv, data = imap.fetch(num, '(RFC822)')
                        print(data)
                        if rv != 'OK':
                            print ("ERROR getting message", num)
                            return
                else:
                    for num in data[0].split()[-batch_size:]:
                        rv, data = imap.fetch(num, '(RFC822)')
                        print(data)
                        if rv != 'OK':
                            print ("ERROR getting message", num)
                            return
                    
                imap.close()
                
                if logout_after:
                    imap.logout()

        except Exception as e:
            raise e

我按这个顺序使用 imap.login,然后是 imap.select,最后是 imap.search,所以我应该离开身份验证状态并进入选定状态,我是否遗漏了什么?

python 电子邮件 imap imaplib

评论

0赞 Max 8/20/2023
确保检查选择结果。如果失败,您将收到该错误。
0赞 arnt 8/20/2023
当您检查 select 结果时,您会注意到返回值包含一个数字,并且搜索的返回值是从 1 到该数字的整数。

答: 暂无答案