如何使用MailKit删除Gmail中的邮件(不移至“废纸篓”文件夹)

时间:2014-04-23 07:32:23

标签: c# gmail imap gmail-imap mailkit

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

如何永久从Gmail帐户中删除邮件,而不是将其移至“已删除邮件”文件夹?

1 个答案:

答案 0 :(得分:11)

在大多数IMAP服务器上,执行此操作的方式是:

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

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

folder.Expunge (uids);

这将清除文件夹中的消息。

假设这在GMail上不起作用,可能是因为只要在GMail IMAP服务器上的消息中添加\Deleted标志,它就会将其移动到Trash文件夹(这是超出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);

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