Python 3 imaplib.fetch 类型错误 : can't concat bytes to int

标签 python imap

我有一些代码可以获取 IMAP 电子邮件并且在 Python 2 中运行良好。在 Python3 中我得到以下错误:

Traceback (most recent call last):
File "./mail.py", line 295, in
item=return_message(x)
File "./mail.py", line 122, in return_message
result, data = mail.fetch(message_id, "(RFC822)")
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 460, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1113, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 883, in _command
data = data + b' ' + arg
TypeError: can't concat bytes to int

return_message 函数的代码:

result, data = mail.fetch(message_id, "(RFC822)")
raw_email = data[0][1]
email_message = email.message_from_string(raw_email)

运行时信息:

3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 01:25:11)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]

最佳答案

message_set 是一个字符串,不是一个整数。来自documentation :

The message_set options to commands below is a string specifying one or more messages to be acted upon. It may be a simple message number ('1'), a range of message numbers ('2:4'), or a group of non-contiguous ranges separated by commas ('1:3,6:9'). A range can contain an asterisk to indicate an infinite upper bound ('3:*').

直接将其转换为字符串就足够了:

result, data = mail.fetch(str(message_id), "(RFC822)")

关于Python 3 imaplib.fetch 类型错误 : can't concat bytes to int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14080629/

相关文章:

bash - 使用 cURL 通过 IMAP 获取和删除消息

php - 为什么我不能在我的 ubuntu 和 windows 中使用 php imap 函数?

python - Tkinter 单选按钮未传递值

PHP IMAP : How to get the correct body?

python - 在使用 OpenCv 进行图像处理期间保留字符顺序

python - 为什么Python不释放内存(mod_wsgi + Django下)

python - (Python 3) imaplib 从 .txt 文件登录

c# imap/pop3 邮件服务器

python - 创建对角稀疏矩阵的有效方法

python - Flask 不会在 html 中播放视频