+2 votes

Hello Limilabs Teams,

First congrats for the products. It is a great one.

I have a question regarding IMAP:
Is it possible to listen to the Deleted (Trash) folder? We need to be notified when a user deletes an email from GMail (or any other email provider), so that our windows service deletes it from our CRM also.

Best Regards,
Arber

by

1 Answer

+1 vote

Gmail uses slightly different approach to deleting items (they are first moved to the trash folder: https://www.limilabs.com/blog/delete-email-permanently-in-gmail)

There are 3 situations you must react to:

  1. Existing email deleted from the Trash folder (restored, moved back to INBOX).
  2. New email moved in Trash folder
  3. Existing email deleted from the Trash folder (deleted permanently).

You can use IDLE to monitor changes in Trash folder:

using (Imap client = new Imap())
{
    client.ConnectSSL("imap.gmail.com");
    client.Login("@gmail.com", "");
    CommonFolders common = new CommonFolders(client.GetFolders());
    client.Select(common.Trash);
    while (true)
    {
        FolderStatus currentStatus = client.Idle(TimeSpan.FromMinutes(0.5));
        Console.WriteLine("Removed: {0}", currentStatus.Expunge.Count);
        Console.WriteLine("Total message count in Trash: {0}", currentStatus.MessageCount);
    }
}

Here are the logs for 3 situations:

Email is restored (moved from Trash to INBOX):

S: 33f2dde6449e4a62 OK [READ-WRITE] [Gmail]/Trash selected. (Success)
C: 5b3efd49fc2448a1 IDLE
S: + idling
S: * 253 EXPUNGE
S: * 252 EXISTS
C: DONE
S: 5b3efd49fc2448a1 OK IDLE terminated (Success)
Removed: 1
Total message count in Trash: 252

Email is deleted (moved from INBOX to Trash):

C: f073fb2114964c39 IDLE
S: + idling
S: * 253 EXISTS
C: DONE
S: f073fb2114964c39 OK IDLE terminated (Success)
Removed: 0
Total message count in Trash: 253

Email is deleted permanently (removed from Trash):

C: 1b3ddc1bc86943fc IDLE
S: + idling
S: * 253 EXPUNGE
S: * 252 EXISTS
C: DONE
S: 1b3ddc1bc86943fc OK IDLE terminated (Success)
Removed: 1
Total message count in Trash: 252

For other email providers, the problem is that email may be deleted without being first moved to the Trash folder. If you plan to monitor INBOX only this is not a problem.

The problem starts if you want to monitor other folders (e.g. INBOX/subfolder) as IDLE allows only one folder to be monitored and NOTIFY extension (which allows to monitor more folders) is not widely supported.

by (296k points)
...