[英]How to delete emails using UID in imaplib python
I'm trying to delete emails by UID.我正在尝试通过 UID 删除电子邮件。 It's a hotmail email account I'm accessing.这是我正在访问的 hotmail 电子邮件帐户。
Here's what I'm doing:这是我在做什么:
imap = imaplib.IMAP4_SSL('imap-mail.outlook.com')
imap.login('my_email@hotmail.com', "password")
resp, _ = imap.select('Inbox')
mbox_response, msgnums = imap.search( None,'FROM', 'email@sender.com')
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')})
print(messages)
I get the following output:我得到以下输出:
[{
'uid': ('OK', [b'1 (UID 111)']),
'uid': ('OK', [b'2 (UID 114)'])
}]
How do I use these UIDs to delete the specific message?如何使用这些 UID 删除特定消息?
I've tried this without success...我试过这个没有成功...
for m in messages:
imap.store(m['uid'],'+X-GM-LABELS', '\\Trash')
I get the following error:我收到以下错误:
TypeError: can't concat tuple to bytes
from imap_tools import MailBox
with MailBox('imap.mail.com').login('test@mail.com', 'pwd', 'INBOX/test') as mailbox:
# DELETE all messages from current folder (INBOX/test)
mailbox.delete([msg.uid for msg in mailbox.fetch()])
https://github.com/ikvk/imap_tools https://github.com/ikvk/imap_tools
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.