简体   繁体   English

Python imaplib:无法从imap注销

[英]Python imaplib: Cannot logout from imap

I wrote a python script to automate some tasks on my mail account. 我写了一个python脚本来自动执行我的邮件帐户中的某些任务。 So, I can login to my yahoo mail account, read, delete (via imap) and send emails (via smtp). 因此,我可以登录到yahoo邮件帐户,阅读,删除(通过imap)和发送电子邮件(通过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. 注意 :在IMAP协议中,注销注销文件夹之前不需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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