Skip to main content
 首页 » 编程设计

Python IMAP4 选择邮箱名称包含空格字符,报错

2025年05月04日26zhengyun_ustc

我正在使用 Python imaplib.IMAP4 连接到 Google 电子邮件。一切都很好,除非我使用 IMAP4.select() 方法使用包含空格字符的邮箱名称。假设我的邮箱名称是“Gama Sutra”。

当我执行 imap.select('[Gmail]/Gama Sutra') 时,它给我这个错误:

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", line 682, in select 
typ, dat = self._simple_command(name, mailbox) 
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", line 1133, in _simple_command 
return self._command_complete(name, self._command(name, *args)) 
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", line 964, in _command_complete 
raise self.error('%s command error: %s %s' % (name, typ, data)) 
imaplib.error: SELECT command error: BAD [b'Could not parse command'] 

我该如何解决这个问题?

请您参考如下方法:

它应该会自动执行此操作,但只需将您的文件夹名称用双引号引起来即可:

imap.select('[Gmail]/Gama Sutra"')