2

I'm working with MailKit lib and have a problem.

My application is read specify email in Inbox (Gmail), and delete them.

IList<UniqueId> listUid = inbox.Search(query);
for (int i = 0; i < listUid.Count; i++)
{
  var message = inbox.GetMessage(listUid[i]);
  inbox.AddFlags(msg.Uid, MessageFlags.Deleted, true);
  inbox.Expunge();
}

It run OK, But when Gmail setting Conversation View = Conversation view on, the message that marked as deletion will comback in Inbox if have same email subject and same sender. In next-time I count message, all of deletion message will be re-count. How to avoid it? (save Uid of deletion message is one way but when message number increase, processing will be slow)

Thanks very much.

Zaheer Ul Hassan
  • 771
  • 9
  • 24
user1234
  • 25
  • 9

1 Answers1

4

GMail unfortunately does not behave the same way most other IMAP servers behave and so when you mark a message as \Deleted, it gets moved to the Trash folder automatically and so the Expunge does nothing.

What you need to do is go to your GMail settings and change the behavior of your IMAP account so that it doesn't move the messages to Trash.

Either that or MoveTo() the message to the Trash folder yourself so you can get the UID of the message in the Trash folder (hint: use the return value of the MoveTo() method) and then open the Trash folder and expunge the message from there.

Note: this code is untested, but it should look something like this:

var trash = client.GetFolder (SpecialFolder.Trash);
var moved = client.Inbox.MoveTo (uid, trash);
if (moved.HasValue) {
    trash.Open (FolderAccess.ReadWrite);
    trash.AddFlags (moved.Value, MessageFlags.Deleted, true);
    trash.Expunge (new [] { moved.Value });
}
jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Thank you. I followed your suggest and resolved this problem. But when I using yahoo email. `var trash = client.GetFolder (SpecialFolder.Trash);` has been throw an exception `The IMAP server does not support the SPECIAL-USE nor XLIST extensions.` With mail server that not support to get special folder, how to do that? – user1234 May 12 '17 at 03:59
  • I think that in this case, I will use `AddFlags` and `Expunge` normally. it's seem working now. – user1234 May 12 '17 at 04:18
  • how about pop3? I know that call `DeleteMessage` to delete a message, but when `Conversation View = Conversation view on` the message that has been deleted will be back in inbox and count it again. how to avoid this? – user1234 May 12 '17 at 06:50
  • Right. Not all servers support SPECIAL-USE and so you can't use the SpecialFolder enum to get the special folders. For servers like that, you'll either have to use the method you were originally using or else have custom logic for determining the Trash folder (perhaps a user specified folder?). For POP3, I'm not sure how to work around that - perhaps there is a GMail setting for that as well? – jstedfast May 12 '17 at 11:31