skip to Main Content

I wrote a programm reading specific messages from gmail. After reading and analysing I want to delete them. However, I can’t get it to work. I use the following code:

    /// <summary>
    /// Deletes a set of E-Mails by their messageID from a given mailbox
    /// </summary>
    /// <param name="messageIds">The IDs of the messages to delete</param>
    /// <param name="mailBox">The mailbox to delete the messages from</param>
    public void DeletMails(List<int> messageIds, string mailBox)
    {
        if (messageIds.Count<1)
        {
            return;
        }
        // get the mailbox
        Mailbox mails = Client.SelectMailbox(mailBox);
        
        // create a collection of flags to set for the emails
        var flags = new FlagCollection();
        // set only the deleted flag
        flags.Add(new Flag("DELETED"));

        Logging.Log.Info($"Deleting {messageIds.Count} mails");
        // set the flag for each email in the messageIds
        try
        {
            foreach (var id in messageIds)
            {
                mails.DeleteMessage(id, true);
                mails.SetFlags(id, flags);
            }
        }
        catch (Exception ex)
        {
            Logging.Log.Error($"{ex.Message}:{ex.StackTrace}");
        }

        Logging.Log.Info($"Finished deleting {messageIds.Count} mails");

        Client.Expunge();
        Client.Disconnect();
    }

I know there mey be some duplicates in the code

  • mails.DeleteMessage(id, true);
  • mails.SetFlags(id, flags);
  • Client.Expunge();

However. It does not work.

When I log into my gmail account, I can still see the emails. Only change: they are marked as read and the label is removed from them. (Example: I label each email from [email protected] with the label PayPal using the filter settings of gmail, then I get all the emails from the mailbox PayPal and remember their ID and delete them. After that they are marked as read and they no longer have the label PayPal, but they are not deleted from the mailbox PayPal.)

There is no exception thrown when executing the code.

Any idea why? Do you recommend using another Mail-API? I am using ActiveUp.Net.Mail.

Thanks for your advice.

2

Answers


  1. Chosen as BEST ANSWER

    Ok, I finally figured it out.

    @Max: The IMAP settings do (semi) work. However, it is a little bit annoying ;) As mentioned here you have to:

    1. set 'When I mark a message in IMAP as deleted' to 'Expunge off – Wait for the client to update the server.'
    2. set 'When a message is marked as deleted and expunged from the last visible IMAP folder' to 'Immediately delete the message forever'

    But as described in the second setting you have to delete the message from all IMAP folders. Google seems to mix folders and labels. So labeling an e-mail with a filter in the gmail settings actually gives this email a secondary label/folder. The first is 'INBOX'. To remove it, so that it doesnt show up in the gmail app, you have to delete the message from INBOX and the other labeled folder with setting the DELETED flag for this message. But you have to do that for the INBOX and the other Folder, where the messages have different IDs.

    Then the message is moved to the Gmail/Trash folder, where it is kept for 30 days or something like that before it is finally deleted.

    Thanks for pushing me in the right direction.


  2. For GMail you need to move them trash (UID MOVE x "[Gmail/Trash]"). Watch out for localization of the folder.

    Gmail considers the Deleted flag to be removing the label from that message, which means deleting the Inbox label is effectively the same as archiving the message.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search