python imaplib UID command error: BAD [b'Could not parse command']

python imaplib UID command error: BAD [b'Could not parse command']

我正在尝试使用复制命令复制邮件。

imapper.copy(email.uid, 'TEST')

email.uid = 本例中的 uid 1069

TEST = 邮件应位于的地图

这是我的复制功能:

def copy(self, uid, to):
    #typ, content = self._mailer.copy(bytes(uid), to)
    print (uid)
    typ, content = self._mailer.uid('copy', bytes(1069), 'TEST')
    if typ == 'OK':
        mail = _parse_email(content, include_raw=include_raw)
        return mail
    else:
        raise Exception("Could not copy email.")

由于出现此错误,我决定不使用变量并对它进行硬编码,直到它工作为止,而不是用变量替换它。

typ, content = self._mailer.uid('copy', bytes(1069), 'TEST')

这个returns错误:

UID command error:BAD [b'Could not parse command']

我已经添加了 bytes() 否则我会得到这个错误:

TypeError: can't concat int to bytes

好的,这就是我的解决方案:

我已将 bytes UID 添加到邮件对象,并将复制函数更改为使用此 bytes uid。这解决了所有问题

我还用我的实现制作了一个 easyimap 包装器的分支

因此有了将邮件复制到另一个邮箱的新功能

https://github.com/UGxMvH/easyimap

它需要一个字符串。使用 str(1069) 或 '1069',而不是整数。尽管它们是数字,但协议将它们视为字符串,而不是数字。

bytes(1069) in python 创建一个包含 1069 个零的字节数组,因此您发送了一堆空值。