{"id":1227,"date":"2010-10-25T15:02:04","date_gmt":"2010-10-25T13:02:04","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1227"},"modified":"2013-06-25T22:28:37","modified_gmt":"2013-06-25T20:28:37","slug":"delete-email-messages-with-imap","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/delete-email-messages-with-imap","title":{"rendered":"Delete email messages with IMAP"},"content":{"rendered":"<p>This article describes how to <strong>delete email messages<\/strong> using Mail.dll <a href=\"\/mail\">.NET IMAP library<\/a> and IMAP protocol.<\/p>\n<p>Internally deleted messages are flagged with \\DELETED flag and are actually deleted after the client issues EXPUNGE command. Mail.dll issues this command automatically.<\/p>\n<p>Following code will find unique ids of all unseen messages in the Inbox folder and <strong>delete them one by one<\/strong>.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing Limilabs.Client.IMAP;\r\nusing Limilabs.Mail;\r\nusing System;\r\nusing System.Collections.Generic;\r\n\r\n\r\nclass Program\r\n{\r\n    static void Main(string&#x5B;] args)\r\n    {\r\n        using (Imap imap = new Imap())\r\n        {\r\n            imap.Connect(&quot;imap.example.com&quot;);    \/\/ use ConnectSSL for SSL.\r\n            imap.Login(&quot;user&quot;, &quot;password&quot;);\r\n\r\n            imap.SelectInbox();\r\n            List&lt;long&gt; uids = imap.Search(Flag.Unseen);\r\n            foreach (long uid in uids)\r\n            {\r\n                imap.DeleteMessageByUID(uid);\r\n            }\r\n            imap.Close();\r\n        }\r\n    }\r\n};\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nImports Limilabs.Mail\r\nImports Limilabs.Client.IMAP\r\nImports System\r\nImports System.Collections.Generic\r\n\r\nPublic Module Module1\r\n    Public Sub Main(ByVal args As String())\r\n\r\n        Using imap As New Imap()\r\n            imap.Connect(&quot;imap.example.com&quot;)    ' use ConnectSSL for SSL.\r\n            imap.Login(&quot;user&quot;, &quot;password&quot;)\r\n\r\n            imap.SelectInbox()\r\n            Dim uids As List(Of Long) = imap.Search(Flag.Unseen)\r\n            For Each uid As Long In uids\r\n                imap.DeleteMessageByUID(uid)\r\n            Next\r\n            imap.Close()\r\n        End Using\r\n\r\n    End Sub\r\nEnd Module\r\n<\/pre>\n<p>You can also use <strong> bulk delete methods<\/strong>, which are much faster when operating on large number of unique ids. This is because EXPUNGE command is issued only once.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing (Imap imap = new Imap())\r\n{\r\n    imap.Connect(&quot;imap.example.com&quot;);    \/\/ use ConnectSSL for SSL.\r\n    imap.Login(&quot;user&quot;, &quot;password&quot;);\r\n\r\n    imap.SelectInbox();\r\n    List&lt;long&gt; uids = imap.Search(Flag.Unseen);\r\n\r\n    imap.DeleteMessageByUID(uids);\r\n\r\n  imap.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nPublic Module Module1\r\n    Public Sub Main(ByVal args As String())\r\n\r\n        Using imap As New Imap()\r\n            imap.Connect(&quot;imap.example.com&quot;)    ' use ConnectSSL for SSL.\r\n            imap.Login(&quot;user&quot;, &quot;password&quot;)\r\n\r\n            imap.SelectInbox()\r\n            Dim uids As List(Of Long) = imap.Search(Flag.Unseen)\r\n\r\n            imap.DeleteMessageByUID(uids)\r\n\r\n            imap.Close()\r\n        End Using\r\n\r\n    End Sub\r\nEnd Module\r\n<\/pre>\n<p>Gmail server behaves a bit differently than most IMAP server please read the <a href=\"\/blog\/delete-email-permanently-in-gmail\">delete emails from Gmail<\/a> for details.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[15,28,77,57],"class_list":["post-1227","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-imap","tag-imap-component","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1227"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=1227"}],"version-history":[{"count":6,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1227\/revisions"}],"predecessor-version":[{"id":4040,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1227\/revisions\/4040"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}