简体   繁体   English

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

[英]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:这是我在做什么:

1. Connect to Email 1. 连接到电子邮件

imap = imaplib.IMAP4_SSL('imap-mail.outlook.com')
imap.login('my_email@hotmail.com', "password")

2. Get UID from emails 2. 从邮件中获取 UID

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')})

3. Print UIDs 3. 打印 UID

print(messages)

I get the following output:我得到以下输出:

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

4. How do I delete? 4. 如何删除?

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.

相关问题 如何使用 python imaplib 阅读电子邮件 - How to read emails using python imaplib 尝试使用 Python 3.7.2 和 IMAPClient 批量删除电子邮件 - imaplib.IMAP4.error: UID 命令错误:BAD [b'命令行太大'] - Trying to mass delete emails using Python 3.7.2 & IMAPClient - imaplib.IMAP4.error: UID command error: BAD [b'Command line too large'] 如何使用 Python imaplib 过滤/搜索带有自定义标头字段的电子邮件? - How to filter/search for emails with a custom header field using Python imaplib? 使用 Python imaplib 永久删除 Gmail 电子邮件 - Permanently delete Gmail email using Python imaplib 使用python imaplib从Gmail“删除”电子邮件? - Using python imaplib to “delete” an email from Gmail? 如何使用Python中的imaplib在给定日期过滤掉多个发件人的电子邮件? - How to I filter out emails from multiple senders on a given date using imaplib in Python? 如果我使用imaplib在收件箱中有此邮件的UID,如何在All Maill文件夹中获取邮件的UID? (GMAIL) - How to get UID of mail in All Maill folder if I have UID of this mail in inbox using imaplib? (GMail) Python:imaplib如何仅获取主要电子邮件(Gmail) - Python : imaplib how to get primary emails only (Gmail) python IMAPlib 如何将没有 UID 的电子邮件移动到子文件夹中 - python IMAPlib how to move emails with no UIDs into sub folders 使用imaplib获取电子邮件的标题? - Using imaplib to get headers of emails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM