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.
December 22nd, 2014 at 20:22
Is there any way, that i can just delete the attachments only and leave the content text of the email?
December 22nd, 2014 at 22:42
@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.
October 24th, 2016 at 21:28
Hi,
Is it possible to delete a message from the Sent folder?
Thanks
October 25th, 2016 at 16:59
@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:
October 27th, 2016 at 21:44
You’re right, thanks!
And DeleteXXX methods will only work with Select folder, not with Examine (which select folder in read-only mode)
September 27th, 2017 at 11:57
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
September 27th, 2017 at 13:02
@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:
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.