3

我正在尝试使用 MailKit 库开发 ImapClient 。

如何从 Gmail 帐户中永久删除邮件,而不是简单地将它们移动到垃圾箱文件夹?

4

1 回答 1

11

在大多数 IMAP 服务器上,您可以这样做:

folder.AddFlags (uids, MessageFlags.Deleted, true);

这会\Deleted在消息上设置标志。下一步将是:

folder.Expunge (uids);

这将从文件夹中清除邮件。

假设这在 GMail 上不起作用,可能是因为一旦您将\Deleted标志添加到 GMail IMAP 服务器上的邮件,它就会将其移动到垃圾箱文件夹(这超出了 IMAP 客户端的控制)。

这是一个可能有效的想法,但是......

// First, get the globally unique message id(s) for the message(s).
var summaries = folder.Fetch (uids, MessageSummaryItems.GMailMessageId);

// Next, mark them for deletion...
folder.AddFlags (uids, MessageFlags.Deleted, true);

// At this point, the messages have been moved to the Trash folder.
// So open the Trash folder...
folder = client.GetFolder (SpecialFolder.Trash);
folder.Open (FolderAccess.ReadWrite);

// Build a search query for the messages that we just deleted...
SearchQuery query = null;
foreach (var message in summaries) {
    var id = SearchQuery.GMailMessageId (message.GMailMessageId);
    query = query != null ? query.Or (id) : id;
}

// Search the Trash folder for these messages...
var matches = folder.Search (query);

// Not sure if you need to mark them for deletion again...
folder.AddFlags (matches, MessageFlags.Deleted, true);

// Now purge them from the Trash folder...
folder.Expunge (matches);

你已经完成了......

于 2014-04-25T19:10:46.060 回答