简体   繁体   中英

Python imaplib: Cannot logout from imap

I wrote a python script to automate some tasks on my mail account. So, I can login to my yahoo mail account, read, delete (via imap) and send emails (via smtp). After that I want to logout. However, I am getting this error, which I do not know how to repair:

This is what happens:

Exception in close_imap
Traceback (most recent call last):
  File "/Users/Tom/MeineDaten/Programmieren/Sportwetten/Tipico/Report-Gambling-Apps/emailing/Mailer.py", line 55, in close_imap
    self.imap.close()
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/imaplib.py", line 445, in close
    typ, dat = self._simple_command('CLOSE')
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/imaplib.py", line 1180, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/imaplib.py", line 928, in _command
    ', '.join(Commands[name])))
imaplib.IMAP4.error: command CLOSE illegal in state LOGOUT, only allowed in states SELECTED

And this is the corresponding code:

import imaplib

...other code here...

def close_imap(self):
    if self.imap is None:
        print("close_imap: self.imap is None. No further action taken. Returning.")
        return
    try:
        self.imap.close()
        self.imap.logout()
    except Exception:
        print("Exception in close_imap")
        print(str(traceback.format_exc()))
        pass

What am I doing wrong here?

You are trying to close a folder but you have not selected any.

This may be due to two thinks:

  • You never selected a folder in this session
  • You already closed the folder but you are trying to close it a second time.

You should only close a folder one time.

Note : This is not necessary in the IMAP protocol to close a folder before logging out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM