如何在 imaplib python 中使用 UID 删除电子邮件

问题描述 投票:0回答:1

我正在尝试通过 UID 删除电子邮件。这是我正在访问的 hotmail 电子邮件帐户。

这就是我正在做的:

1.连接到电子邮件

imap = imaplib.IMAP4_SSL('imap-mail.outlook.com')
imap.login('[email protected]', "password")

2.从电子邮件中获取UID

resp, _ = imap.select('Inbox')
mbox_response, msgnums = imap.search( None,'FROM', '[email protected]')

messages = [] #Appending UID to this dictionary

for num in msgnums[0].split():
    msg_uid = imap.fetch(num, 'UID')
    messages.append({'uid':imap.fetch(num, 'UID')})

3.打印 UID

print(messages)

我得到以下输出:

[{
  'uid': ('OK', [b'1 (UID 111)']), 
  'uid': ('OK', [b'2 (UID 114)'])
}]

4.怎么删除?

如何使用这些 UID 来删除特定消息?

我已经尝试过但没有成功...

for m in messages:
    imap.store(m['uid'],'+X-GM-LABELS', '\\Trash')

我收到以下错误:

TypeError: can't concat tuple to bytes
python imap imaplib
1个回答
-1
投票
from imap_tools import MailBox, A
with MailBox('imap.mail.com').login('[email protected]', 'pwd', 'INBOX/test') as mailbox:
    # DELETE all flagged messages from current folder (INBOX/test)
    mailbox.delete(mailbox.uids(A(flagged=True)))

https://github.com/ikvk/imap_tools

阅读 lib 文档了解详细信息

© www.soinside.com 2019 - 2024. All rights reserved.