Page 1 of 1

Unable to delete message from IMAP folder view

Posted: 2022-11-24 18:18
by Rogue404
I have a C# program using the COM API that looks at a "blocked" IMAP folder to automatically unsubscribe and create a rule to block the email sender in the future.

When a user moves the email into the folder and the script runs - I would like to deleted the email from the folder to indicated that it has been processed. I have tried setting the message flag to deleted and delete by ID, but the message still shows up in the IMAP folder. When I rerun the script it does not detect that there are any emails in that folder so the message is being deleted. So what does the client IMAP folder key off from that is it still showing the message as not deleted? As I can use two different IMAP clients and delete an email on one and it will "almost" immediately be deleted from the other client? (Also, when I restart the IMAP Client - the email is still there)

Code: Select all

   hMailServer.IMAPFolder folder = account.IMAPFolders.ItemByName["block"];

   Console.WriteLine(" => Messages: " + folder.Messages.Count.ToString());

   for (int m = folder.Messages.Count - 1; m >= 0; m--)
   {
      hMailServer.Message message = folder.Messages[m];

      records.Add(ProcessMessage(message));

      Console.WriteLine(" Deleting message ID: " + message.ID.ToString() );

      message.Flag[eMessageFlag.eMFDeleted] = true;
                            
      folder.Messages.DeleteByDBID(message.ID);
   }

Re: Unable to delete message from IMAP folder view

Posted: 2022-11-24 21:17
by palinka
DeleteByDBID() will permanently delete the message, so changing the flag won't make any difference at all. Its redundant.
Rogue404 wrote:
2022-11-24 18:18
I have tried setting the message flag to deleted and delete by ID, but the message still shows up in the IMAP folder. When I rerun the script it does not detect that there are any emails in that folder so the message is being deleted.
I don't understand what this means. Is the message deleted or not?
So what does the client IMAP folder key off from that is it still showing the message as not deleted?
Its probably cache of some kind. I would not use the client to determine whether your script is working.

Code: Select all

   hMailServer.IMAPFolder folder = account.IMAPFolders.ItemByName["block"];

   Console.WriteLine(" => Messages: " + folder.Messages.Count.ToString());

   for (int m = folder.Messages.Count - 1; m >= 0; m--)
   {
      hMailServer.Message message = folder.Messages[m];

      records.Add(ProcessMessage(message));

      Console.WriteLine(" Deleting message ID: " + message.ID.ToString() );

      message.Flag[eMessageFlag.eMFDeleted] = true;
                            
      folder.Messages.DeleteByDBID(message.ID);
   }
I don't know anything about C#, but the hmailserver api part looks good. It should be working.

If you want to see if the message exists, just pull it up: (folder.Messages.ItemByDBID(message.ID)).Filename

If it exists, you'll get the filename. If not, you'll get nothing or maybe an error. Either way, you'll know whether your script is working.