C# 如何使用MailKit删除Gmail中的邮件(不移动到垃圾文件夹)

C# 如何使用MailKit删除Gmail中的邮件(不移动到垃圾文件夹),c#,gmail,imap,gmail-imap,mailkit,C#,Gmail,Imap,Gmail Imap,Mailkit,我正在尝试使用MailKit库开发一个ImapClient 如何从Gmail帐户中永久删除邮件,而不是简单地将邮件移动到垃圾文件夹中?在大多数IMAP服务器上,这样做的方式是: folder.AddFlags (uids, MessageFlags.Deleted, true); 这将设置消息上的\Deleted标志。下一步将是: folder.Expunge (uids); 这将从文件夹中清除邮件 假设这在GMail上不起作用,可能是因为一旦你在GMail IMAP服务器上的邮件中添加了\

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


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

在大多数IMAP服务器上,这样做的方式是:

folder.AddFlags (uids, MessageFlags.Deleted, true);
这将设置消息上的
\Deleted
标志。下一步将是:

folder.Expunge (uids);
这将从文件夹中清除邮件

假设这在GMail上不起作用,可能是因为一旦你在GMail IMAP服务器上的邮件中添加了
\Deleted
标志,它就会将邮件移动到垃圾箱文件夹(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);
你已经完成了