IMAP4LIB使用store命令时,出现错误“ BAD [b'无法解析命令']”

时间:2020-04-17 02:43:58

标签: python-3.x imap

我对所有这些都是陌生的,所以如果我搞砸了或者已经搞砸了,对不起。我有两个类GUI和我的GUI类中的MailSorter类,我有一个可以登录的方法,然后一个类提取所有EmailId,然后最后提取所有发件人电子邮件并将其存储在字典中。其中存储“发件人”电子邮件及其显示的时间,以及一个包含“发件人”电子邮件和ID的数组。

    def fetchFrom(self,emailIDs):
        EmailAmount = dict()
        Email = []
        count = 0
        for emailId in emailIDs:
            #Converts email into string
            result2,email_data = self.mail.fetch(emailId,'(RFC822)')
            try:
                raw_email = email_data[0][1].decode("utf-8")
                email_message = email.message_from_string(raw_email)
                #Fetches email address sent from
                From = email_message["From"]
                Email.append((From,emailId))
                #print(From)
                if From in EmailAmount:
                    EmailAmount[From] = EmailAmount[From] + 1
                else:
                    EmailAmount[From] = 1
                count += 1
                if count > 10:
                    break
            except Exception as e:
                self.log.append((emailId,e))
    def mainScreenInterface(self):
        #Process
        print("Loading program")
        EmailIds = self.Mail.fetchEmailId()
        EmailDict, self.EmailArray = self.Mail.fetchFrom(EmailIds)

        self.master.geometry("750x600")
        self.master.title("Main Screen")
        self.destoryWidget()

        #New Frame
        self.mainScreen = tk.Frame(self.master)
        self.mainScreen.pack()

        #Labels
        mainText = tk.Label(self.mainScreen,text = "All Emails")
        mainText.config(font=("Courier","25"))

        #Buttons
        delete = tk.Button(self.mainScreen,text="Delete", command = self.Delete)
        deleteAll = tk.Button(self.mainScreen,text="Delete All", command = self.DeleteAll) 
        Help = tk.Button(self.mainScreen,text="Help", command = self.Help_)

        #Scrollbar
        scrollbar = tk.Scrollbar(root)
        scrollbar.pack(side="right",fill="y")

        #Listbox
        self.listbox = tk.Listbox(root,width = root.winfo_screenwidth(), height = 25)
        #Attach a scrool wheel to the listbox
        self.listbox.config(yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.listbox.yview)

        #Add items to the list box
        count = 1
        for x,y in EmailDict.items():
            self.listbox.insert(count,(x,y))
            count += 1

        #Placement
        paddingValue = 40
        mainText.pack(side="top")
        self.listbox.pack(side="top")
        delete.pack(side="left",padx=paddingValue)
        deleteAll.pack(side="left",padx=paddingValue)
        Help.pack(side="left",padx=paddingValue)
    def Delete(self):
        emailName = self.listbox.get(tk.ANCHOR)[0]
        self.Mail.deleteEmail(emailName,self.EmailArray)

所以fetchFrom来自mailSorter类,另外两个来自GUI类,当我调用deleteEmail时出现错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\******\Desktop\Email Sorter v3.py", line 197, in Delete
    self.Mail.deleteEmail(emailName,self.EmailArray)
  File "C:\Users\******\Desktop\Email Sorter v3.py", line 66, in deleteEmail
    self.mail.store(Id[1].strip(), '+X-GM-tk.LabelS', '\\Trash')
  File "C:\Python\lib\imaplib.py", line 840, in store
    typ, dat = self._simple_command('STORE', message_set, command, flags)
  File "C:\Python\lib\imaplib.py", line 1196, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Python\lib\imaplib.py", line 1027, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.IMAP4.error: STORE command error: BAD [b'Could not parse command']

但是当我将其作为没有GUI的文本库运行并使用示例电子邮件时,一切正常:

    test = MailSorter("hamadnassor5@gmail.com","snerfulbubble1.")
    test.login()
    EmailIds = test.fetchEmailId()
    EmailDict, EmailArray = test.fetchFrom(EmailIds)
    test.displayEmails(EmailDict)
    test.deleteEmail("Xbox <Xbox@outlook.com>",EmailArray)
    test.closeCon()

1 个答案:

答案 0 :(得分:0)

DeleteMail代码

    def deleteEmail(self, emailName, EmailArray):
        for Id in EmailArray:
            if Id[0] == emailName:
                print(Id[0])
                print(emailName)
                print(Id[1])
                self.mail.store(Id[1].strip(), '+X-GM-tk.LabelS', '\\Trash')
相关问题