jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.
http://www.mimekit.net
MIT License
6.04k stars 809 forks source link

Cannot delete message from IMPAP account. #1738

Closed louisoft88 closed 3 months ago

louisoft88 commented 3 months ago

I use .Net 4.8 and MimeKit 4.4.0.0.

The following code does not delete any email from my IMAP account.

private async Task<int> DeleteSelectEmailsFromAccount(Account account)
{
...
...
   using (var client = new ImapClient())
  {
      client.Connect(account.HostName, account.Port, account.UseSsl);
      client.Authenticate(account.Email, account.Password);

      var inbox = client.Inbox;
      await inbox.OpenAsync(FolderAccess.ReadWrite);

      inbox.AddFlags(emailListItem.EmailMessage.Uid, MessageFlags.Deleted, true);
      inbox.Expunge();
  }

  client.Disconnect(true);
}

How can I delete an email from an IMAP account's inbox? Thanks, Louis

jstedfast commented 3 months ago

Why is client.Disconnect(true) outside of the using block?

Why are you using inbox.OpenAsync() but all other methods you call the sync API?

The AddFlags() and Expunge() should delete the message. If they don't, I'll need a protocol log to diagnose the issue.

louisoft88 commented 3 months ago

Thanks for the very quick reply.

(Congratulations! MailKit is an excellent tool!!!)

Async: I could not delete the message so I tried everything. THanks for notify me.

Explunge(): Typo, sorry I just wanted to make the code more readable.

I attached a log file, when I wanted to delete a message.

When I delete a message then it disappears from my email list on the screen (I remove it) but getting messages next time shows the "deleted" message again. I checked it with my webmail app. The "deleted" ImapLog.txt file was not deleted.

Many thanks, Louis

jstedfast commented 3 months ago

@louisoft88 it looks like it worked based on the logs:

C: C00000006 STORE 1 +FLAGS (\Deleted)
S: * 1 FETCH (FLAGS ($IsMailingList \DELETED) UID 2277)
S: C00000006 OK STORE Command Completed
C: C00000007 EXPUNGE
S: * 1 EXPUNGE
S: C00000007 OK EXPUNGE completed
C: C00000008 LOGOUT

And then you connect to the same server again, open the inbox, and have 0 messages just as expected:

D00000005 SELECT INBOX
S: * FLAGS (\Deleted \Seen \Answered \Flagged \Draft $Forwarded $IsMailingList)
S: * 0 EXISTS
S: * 0 RECENT
S: * OK [PERMANENTFLAGS (\Deleted \Seen \Answered \Flagged \Draft $Forwarded $IsMailingList)]
S: * OK [UIDNEXT 83]
S: * OK [UIDVALIDITY 1696508062] UIDs valid
S: D00000005 OK [READ-WRITE] SELECT completed
C: D00000006 LOGOUT
S: * BYE IMAP4rev1 server terminating connection
S: D00000006 OK LOGOUT Initiated
louisoft88 commented 3 months ago

Thanks, it works now, it was my fault. I am sorry about this issue. Louis