Search code examples
c#.netimapgmail-imapinterimap

How to Empty Gmail Trash with C#


I tried to access my Gmail mailbox with InterImap Library. It shows my messages there, but unable to delete messages. I also found a method "EmptyFolder()" but doesn't work.

All that worked for me is MoveMessageToFolder() but it's not that I need.

Please help me empty my Trash with C# using the same or any other library. I need the code sample that does that.

Here's the code what I managed to write.

var config = new InterIMAP.IMAPConfig("imap.gmail.com", "<my gmail username>", "<my gmail password", true, true, "");
var client = new InterIMAP.Synchronous.IMAPClient(config, new InterIMAP.IMAPLogger(config, new object[] { }), 1);
var trash = client.Folders["[Gmail]"].SubFolders["Trash"];
trash.EmptyFolder();
client.Logoff();

Thanks in Advance.


Solution

  • If you delete a message from your Inbox or one of your custom folders, it will still appear in [Gmail]/All Mail.

    Here’s why: in most cases, deleting a message simply removes that folder’s label from the message, including the label identifying the message as being in your Inbox.

    [Gmail]/All Mail shows all of your messages, whether or not they have labels attached to them.

    If you want to permanently delete a message from all folders:

    1. Move it to the [Gmail]/Trash folder.
    2. Delete it from the [Gmail]/Trash folder.

    You can find more details here: http://www.limilabs.com/blog/delete-email-permanently-in-gmail