{"id":1245,"date":"2010-10-25T15:11:56","date_gmt":"2010-10-25T13:11:56","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1245"},"modified":"2013-06-25T22:49:51","modified_gmt":"2013-06-25T20:49:51","slug":"folder-management-using-imap-create-delete-rename","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/folder-management-using-imap-create-delete-rename","title":{"rendered":"Folder management using IMAP (create, delete, rename)"},"content":{"rendered":"<p>Here&#8217;s the simple example that creates, renames and deletes an IMAP folder using Mail.dll <a href=\"\/mail\">IMAP library<\/a>:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version:\r\n\r\nusing Limilabs.Mail;\r\nusing Limilabs.Client.IMAP;\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;server.company.com&quot;);\r\n           imap.Login(&quot;user&quot;, &quot;password&quot;);\r\n\r\n           imap.SelectInbox();\r\n\r\n           imap.CreateFolder(&quot;New folder&quot;);\r\n           imap.RenameFolder(&quot;New folder&quot;, &quot;Better name&quot;);\r\n           imap.DeleteFolder(&quot;Better name&quot;);\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 version:\r\n\r\nImports Limilabs.Mail\r\nImports Limilabs.Client.IMAP\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;server.company.com&quot;)\r\n            imap.Login(&quot;user&quot;, &quot;password&quot;)\r\n\r\n            imap.SelectInbox()\r\n\r\n            imap.CreateFolder(&quot;New folder&quot;)\r\n            imap.RenameFolder(&quot;New folder&quot;, &quot;Better name&quot;)\r\n            imap.DeleteFolder(&quot;Better name&quot;)\r\n\r\n            imap.Close()\r\n        End Using\r\n\r\n    End Sub\r\nEnd Module\r\n\r\n<\/pre>\n<h2>Checking if folder exist<\/h2>\n<p>The simplest way is to get a list with all folders and check if it contains the name you are looking for. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nList&lt;FolderInfo&gt; all = imap.GetFolders();\r\nbool exists = all.Find(x =&gt; x.Name == &quot;INBOX\/Test folder&quot;) != null;\r\n<\/pre>\n<p>Mail.dll uses <em>FolderInfo<\/em> class to represent IMAP folder. It contains much useful folder information such as <em>Name<\/em>, <em>Flags<\/em>, <em>HasChildren<\/em> and <em>CanSelect<\/em> properties.<\/p>\n<h2>Creating IMAP folder<\/h2>\n<p>Creating folder is easy and not related to currently selected folder. Folder is always created at root.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nimap.CreateFolder(&quot;Test folder&quot;);\r\n<\/pre>\n<p>To create nested folder, you need to know IMAP server&#8217;s separator character. Most common characters are: &#8216;\/&#8217; (slash) and &#8216;.&#8217; (dot):<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nimap.CreateFolder(&quot;INBOX\/Test folder&quot;);\r\n<\/pre>\n<p>Every <em>FolderInfo<\/em> instance contains <em>SeparatorCharacter<\/em> property:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nchar separatorCharacter = (char)imap.GetFolders()&#x5B;0].SeparatorCharacter;\r\n<\/pre>\n<h2>List sub folders<\/h2>\n<p>Listing <strong>direct sub folders<\/strong>:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nList&lt;FolderInfo&gt; direct = imap.GetFoldersOneLevelDown(&quot;INBOX&quot;);\r\n<\/pre>\n<p>Listing <strong>all sub folders<\/strong>:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nList&lt;FolderInfo&gt; all = imap.GetFolders(&quot;INBOX&quot;);\r\n<\/pre>\n<h2>Get message count<\/h2>\n<p>If you want to get the number of messages in the folder you can use <em>FolderStatus<\/em> class returned by <em>Imap.Examine<\/em> and <em>Imap.Select<\/em> methods<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nFolderStatus status = imap.Examine(&quot;folder&quot;);\r\nlong count = status.MessageCount;\r\n<\/pre>\n<p>To get unseen message count you need to use <a href=\"\/blog\/how-to-search-imap-in-net\">IMAP search<\/a>, and simply check the Count property of the returned uid list.<\/p>\n<h2>Copying, moving email messages<\/h2>\n<p>This next sample will shoe how to copy all messages from one folder to another:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nimap.Select(&quot;INBOX\/Source folder&quot;);\r\nList&lt;long&gt; uids = imap.GetAll();\r\nimap.CopyByUID(uids, &quot;INBOX\/Destination folder&quot;);\r\n<\/pre>\n<p>You can use Mail.dll <a href=\"\/mail\">.NET IMAP library<\/a> to move messages between folders:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nimap.Select(&quot;INBOX\/Source folder&quot;);\r\nList&lt;long&gt; uids = imap.GetAll();\r\nimap.MoveByUID(uids, &quot;Inbox\/Destination folder&quot;);\r\n<\/pre>\n<h2>Deleting IMAP folder<\/h2>\n<p>Deleting IMAP folder is simple, as it does not need to be empty before the operation:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nimap.DeleteFolder(&quot;INBOX\/folder to delete&quot;);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s the simple example that creates, renames and deletes an IMAP folder using Mail.dll IMAP library: \/\/ C# version: using Limilabs.Mail; using Limilabs.Client.IMAP; class Program { static void Main(string&#x5B;] args) { using (Imap imap = new Imap()) { imap.Connect(&quot;server.company.com&quot;); imap.Login(&quot;user&quot;, &quot;password&quot;); imap.SelectInbox(); imap.CreateFolder(&quot;New folder&quot;); imap.RenameFolder(&quot;New folder&quot;, &quot;Better name&quot;); imap.DeleteFolder(&quot;Better name&quot;); imap.Close(); } } }; &#8216; [&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-1245","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\/1245"}],"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=1245"}],"version-history":[{"count":5,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1245\/revisions"}],"predecessor-version":[{"id":3483,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1245\/revisions\/3483"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}