• <legend id='H9whF'><style id='H9whF'><dir id='H9whF'><q id='H9whF'></q></dir></style></legend>

          <bdo id='H9whF'></bdo><ul id='H9whF'></ul>

        <small id='H9whF'></small><noframes id='H9whF'>

      1. <tfoot id='H9whF'></tfoot>
        <i id='H9whF'><tr id='H9whF'><dt id='H9whF'><q id='H9whF'><span id='H9whF'><b id='H9whF'><form id='H9whF'><ins id='H9whF'></ins><ul id='H9whF'></ul><sub id='H9whF'></sub></form><legend id='H9whF'></legend><bdo id='H9whF'><pre id='H9whF'><center id='H9whF'></center></pre></bdo></b><th id='H9whF'></th></span></q></dt></tr></i><div id='H9whF'><tfoot id='H9whF'></tfoot><dl id='H9whF'><fieldset id='H9whF'></fieldset></dl></div>
      2. 无法从收件箱以外的任何文件夹中检索 gmail 邮件(Python3 问题)

        时间:2023-10-20
        • <tfoot id='Fm53p'></tfoot>
            • <bdo id='Fm53p'></bdo><ul id='Fm53p'></ul>

              <legend id='Fm53p'><style id='Fm53p'><dir id='Fm53p'><q id='Fm53p'></q></dir></style></legend>

              1. <small id='Fm53p'></small><noframes id='Fm53p'>

                    <tbody id='Fm53p'></tbody>
                  <i id='Fm53p'><tr id='Fm53p'><dt id='Fm53p'><q id='Fm53p'><span id='Fm53p'><b id='Fm53p'><form id='Fm53p'><ins id='Fm53p'></ins><ul id='Fm53p'></ul><sub id='Fm53p'></sub></form><legend id='Fm53p'></legend><bdo id='Fm53p'><pre id='Fm53p'><center id='Fm53p'></center></pre></bdo></b><th id='Fm53p'></th></span></q></dt></tr></i><div id='Fm53p'><tfoot id='Fm53p'></tfoot><dl id='Fm53p'><fieldset id='Fm53p'></fieldset></dl></div>
                  本文介绍了无法从收件箱以外的任何文件夹中检索 gmail 邮件(Python3 问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  更新:我的代码在 python 2.6.5 下运行,但在 python 3 下运行(我使用的是 3.4.1).

                  Update: my code works under python 2.6.5 but not python 3 (I'm using 3.4.1).

                  我无法在所有邮件"或已发送邮件"文件夹中搜索邮件 - 我遇到了异常:

                  I'm unable to search for messages in the "All Mail" or "Sent Mail" folders - I get an exception:

                  imaplib.error: SELECT command error: BAD [b'Could not parse command']
                  

                  我的代码:

                  import imaplib
                  m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
                  m.login("myemail@gmail.com","mypassword")
                  m.select("[Gmail]/All Mail")
                  

                  使用 m.select("[Gmail]/Sent Mail") 也不起作用.

                  但是从收件箱中读取是有效的:

                  But reading from the inbox works:

                  import imaplib
                  m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
                  m.login("myemail@gmail.com","mypassword")
                  m.select("inbox")
                  ...
                  

                  我使用了 mail.list() 命令来验证文件夹名称是否正确:

                  I used the mail.list() command to verify the folder names are correct:

                  b'(\HasNoChildren) "/" "INBOX"', 
                  b'(\Noselect \HasChildren) "/" "[Gmail]"',
                  b'(\HasNoChildren \All) "/" "[Gmail]/All Mail"', 
                  b'(\HasNoChildren \Drafts) "/" "[Gmail]/Drafts"', 
                  b'(\HasNoChildren \Important) "/" "[Gmail]/Important"', 
                  b'(\HasNoChildren \Sent) "/" "[Gmail]/Sent Mail"', 
                  b'(\HasNoChildren \Junk) "/" "[Gmail]/Spam"', 
                  b'(\HasNoChildren \Flagged) "/" "[Gmail]/Starred"', 
                  b'(\HasNoChildren \Trash) "/" "[Gmail]/Trash"'
                  

                  我正在关注这些问题的解决方案,但它们对我不起作用:
                  imaplib -Gmail 中存档/所有邮件的正确文件夹名称是什么?

                  I'm following the solutions from these questions, but they don't work for me:
                  imaplib - What is the correct folder name for Archive/All Mail in Gmail?

                  我无法在 Gmail 中搜索已发送的电子邮件使用 Python

                  这是一个完整的示例程序,不适用于 Python 3:

                  Here is a complete sample program that doesn't work on Python 3:

                  import imaplib
                  import email
                  
                  m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
                  m.login("myemail@gmail.com","mypassword")
                  m.select("[Gmail]/All Mail")
                  
                  result, data = m.uid('search', None, "ALL") # search all email and return uids
                  if result == 'OK':
                      for num in data[0].split():
                          result, data = m.uid('fetch', num, '(RFC822)')
                          if result == 'OK':
                              email_message = email.message_from_bytes(data[0][1])    # raw email text including headers
                              print('From:' + email_message['From'])
                  
                  m.close()
                  m.logout()
                  

                  抛出以下异常:

                  Traceback (most recent call last):
                  File "./eport3.py", line 9, in <module>
                  m.select("[Gmail]/All Mail")
                  File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 682, in select
                  typ, dat = self._simple_command(name, mailbox)
                  File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 1134, in _simple_command
                  return self._command_complete(name, self._command(name, *args))
                  File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 965, 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']
                  

                  这是适用的相应 Python 2 版本:

                  import imaplib
                  import email
                  
                  m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
                  m.login("myemail@gmail.com","mypassword")
                  m.select("[Gmail]/All Mail")
                  
                  result, data = m.uid('search', None, "ALL") # search all email and return uids
                  if result == 'OK':
                      for num in data[0].split():
                          result, data = m.uid('fetch', num, '(RFC822)')
                          if result == 'OK':
                              email_message = email.message_from_string(data[0][1])    # raw email text including headers
                              print 'From:' + email_message['From']
                  
                  m.close()
                  m.logout()
                  

                  推荐答案

                  正如 这个答案:

                  尝试使用 m.select('"[Gmail]/All Mail"'),以便传输双引号.我怀疑 imaplib 没有正确引用字符串,因此服务器得到了两个参数:[Gmail]/All 和 Mail.

                  Try using m.select('"[Gmail]/All Mail"'), so that the double quotes get transmitted. I suspect imaplib is not properly quoting the string, so the server gets what looks like two arguments: [Gmail]/All, and Mail.

                  它适用于python v3.4.1

                  import imaplib
                  import email
                  
                  m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
                  m.login("myemail@gmail.com","mypassword")
                  m.select('"[Gmail]/All Mail"')
                  
                  result, data = m.uid('search', None, "ALL") # search all email and return uids
                  if result == 'OK':
                      for num in data[0].split():
                      result, data = m.uid('fetch', num, '(RFC822)')
                      if result == 'OK':
                          email_message = email.message_from_bytes(data[0][1])    # raw email text including headers
                          print('From:' + email_message['From'])
                  
                  m.close()
                  m.logout()
                  

                  这篇关于无法从收件箱以外的任何文件夹中检索 gmail 邮件(Python3 问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 Python 解析 Gmail 并将所有早于日期的内容标记为“已读" 下一篇:我无法使用 Python 在 Gmail 中搜索已发送的电子邮件

                  相关文章

                  <tfoot id='0cBFm'></tfoot>
                    <legend id='0cBFm'><style id='0cBFm'><dir id='0cBFm'><q id='0cBFm'></q></dir></style></legend>
                      <bdo id='0cBFm'></bdo><ul id='0cBFm'></ul>

                    1. <small id='0cBFm'></small><noframes id='0cBFm'>

                    2. <i id='0cBFm'><tr id='0cBFm'><dt id='0cBFm'><q id='0cBFm'><span id='0cBFm'><b id='0cBFm'><form id='0cBFm'><ins id='0cBFm'></ins><ul id='0cBFm'></ul><sub id='0cBFm'></sub></form><legend id='0cBFm'></legend><bdo id='0cBFm'><pre id='0cBFm'><center id='0cBFm'></center></pre></bdo></b><th id='0cBFm'></th></span></q></dt></tr></i><div id='0cBFm'><tfoot id='0cBFm'></tfoot><dl id='0cBFm'><fieldset id='0cBFm'></fieldset></dl></div>