Delete email messages with IMAP

This article describes how to delete email messages using Mail.dll .NET IMAP library and IMAP protocol.

Internally deleted messages are flagged with \DELETED flag and are actually deleted after the client issues EXPUNGE command. Mail.dll issues this command automatically.

Following code will find unique ids of all unseen messages in the Inbox folder and delete them one by one.

// C#

using Limilabs.Client.IMAP;
using Limilabs.Mail;
using System;
using System.Collections.Generic;


class Program
{
    static void Main(string[] args)
    {
        using (Imap imap = new Imap())
        {
            imap.Connect("imap.example.com");    // use ConnectSSL for SSL.
            imap.Login("user", "password");

            imap.SelectInbox();
            List<long> uids = imap.Search(Flag.Unseen);
            foreach (long uid in uids)
            {
                imap.DeleteMessageByUID(uid);
            }
            imap.Close();
        }
    }
};
' VB.NET

Imports Limilabs.Mail
Imports Limilabs.Client.IMAP
Imports System
Imports System.Collections.Generic

Public Module Module1
    Public Sub Main(ByVal args As String())

        Using imap As New Imap()
            imap.Connect("imap.example.com")    ' use ConnectSSL for SSL.
            imap.Login("user", "password")

            imap.SelectInbox()
            Dim uids As List(Of Long) = imap.Search(Flag.Unseen)
            For Each uid As Long In uids
                imap.DeleteMessageByUID(uid)
            Next
            imap.Close()
        End Using

    End Sub
End Module

You can also use bulk delete methods, which are much faster when operating on large number of unique ids. This is because EXPUNGE command is issued only once.

// C#

using (Imap imap = new Imap())
{
    imap.Connect("imap.example.com");    // use ConnectSSL for SSL.
    imap.Login("user", "password");

    imap.SelectInbox();
    List<long> uids = imap.Search(Flag.Unseen);

    imap.DeleteMessageByUID(uids);

  imap.Close();
}
' VB.NET

Public Module Module1
    Public Sub Main(ByVal args As String())

        Using imap As New Imap()
            imap.Connect("imap.example.com")    ' use ConnectSSL for SSL.
            imap.Login("user", "password")

            imap.SelectInbox()
            Dim uids As List(Of Long) = imap.Search(Flag.Unseen)

            imap.DeleteMessageByUID(uids)

            imap.Close()
        End Using

    End Sub
End Module

Gmail server behaves a bit differently than most IMAP server please read the delete emails from Gmail for details.

Tags:    

Questions?

Consider using our Q&A forum for asking questions.

7 Responses to “Delete email messages with IMAP”

  1. Asdrubal Says:

    Is there any way, that i can just delete the attachments only and leave the content text of the email?

  2. Limilabs support Says:

    @Asdrubal
    No, it is not possible with IMAP nor POP3 protocols

    You could create new email with the same content, without attachments, and upload it to the server.

    You can use IMail.RemoveAttachments method for that.

  3. Horia Says:

    Hi,

    Is it possible to delete a message from the Sent folder?

    Thanks

  4. Limilabs support Says:

    @Horia,

    Of course it is possible to delete message from the ‘sent’ folder.
    First you need to select it, find the UID or number of the email message you want to delete and use one of the delete methods:

    CommonFolders common = new CommonFolders(imap.GetFolders());
    imap.Select(common.Sent);
    // if you know the exact folder name you can use it instead:
    // imap.Select("Sent");
    
    List<long> uids = imap.Search(
        Expression.Subject("<subject of the message to delete>"));
    
    if (uids.Count > 0)
        imap.DeleteMessageByUID(uids[0]);
    
  5. Horia Says:

    You’re right, thanks!

    And DeleteXXX methods will only work with Select folder, not with Examine (which select folder in read-only mode)

  6. Luca Says:

    Hi!

    I’m trying your mail component and I must say it’s very powerful and simply to use!

    I have a little question.
    How can I delete a single mail (pop or imap I think it’s the same) starting from a IMail object? How can I retrieve the message UID to perform the DeleteByMessageUID method?

    If it’s not possible I can “cache” a collection of the mail and the UID, but i think it’s not the best solution…

    Thanks in advance
    LDM

  7. Limilabs support Says:

    @Luca,

    Thanks for the kind words!

    IMail object doesn’t store unique ID (UID) used by IMAP or POP3 protocol. There are several reasons why it so:

    • UID in IMAP is of type long, while POP3 protocol may use almost any string as UID;
    • Message (IMail) may be retrieved from POP3 and IMAP using message number (UID is unknown then, or even may be unsupported by POP3 server);
    • Message (IMail) may be created from file or eml string retrieved from disk (There is no UID at all in such case);
    • We try to keep email/mime parsing and email protocol implementations (IMAP, SMTP, POP3) as separated as possible.

    The bottom line is, that you’ll need to store the UID using your code. I’d advice creating simple class (named IMAPMail or Pop3Mail), that has UID value and IMail object as its properties.