{"id":3215,"date":"2012-07-04T23:26:01","date_gmt":"2012-07-04T21:26:01","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=3215"},"modified":"2015-08-05T11:20:13","modified_gmt":"2015-08-05T09:20:13","slug":"thread-emails-using-imap","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/thread-emails-using-imap","title":{"rendered":"Thread emails using IMAP"},"content":{"rendered":"<p>The THREAD extension to the IMAP protocol provide a means of <strong>server-based threading of messages<\/strong>, without requiring that the <a href=\"\/mail\">IMAP component<\/a> download the necessary data to do so itself.<\/p>\n<p>Following sample shows how to use <em>Imap <\/em><em>Thread <\/em>method to thread all emails available in the mailbox. <\/p>\n<ul>\n<li>First it checks if THREAD <a href=\"\/blog\/get-supported-server-extensions-imap-pop3-smtp\">extension is available on the server<\/a> (defined in <a href=\"\/mail\/rfc\/5256\">RFC 5256<\/a>).<\/li>\n<li>It also downloads envelopes of all email messages to get messages subjects and basic data.<\/li>\n<li>Finally recursive <em>ShowThread <\/em>method is used. It displays message UID and subject in a formatted tree manner.<\/li>\n<\/ul>\n<p>There is one interesting thing in this sample: you can use any <a href=\"\/blog\/how-to-search-imap-in-net\">search criterion to get messages from the IMAP server<\/a>. For example you could only get unseen messages or messages that are unseen and contain a certain word.<\/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.org&quot;);    \/\/ or ConnectSSL\r\n    imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n    \r\n    ThreadMethod method = ThreadMethod.ChooseBest(imap.SupportedThreadMethods());\r\n    if (method == null)\r\n        throw new Exception(&quot;This server does not support any threading algorithm.&quot;);\r\n\r\n    imap.SelectInbox();\r\n\r\n    List&lt;MessageThread&gt; threads = imap.Thread(method).Where(Expression.All());\r\n    List&lt;Envelope&gt; envelopes = imap.GetEnvelopeByUID(\r\n        imap.Search().Where(Expression.All()));\r\n\r\n    foreach (MessageThread thread in threads)\r\n    {\r\n        ShowThread(0, thread, envelopes);\r\n    }\r\n    imap.Close();\r\n}\r\n\r\npublic void ShowThread(int level, MessageThread thread, List&lt;Envelope&gt; envelopes)\r\n{\r\n    string indent = new string(' ', level*4);\r\n    string subject = envelopes.Find(x =&gt; x.UID == thread.UID).Subject;\r\n    Console.WriteLine(&quot;{0}{1}: {2}&quot;, indent, thread.UID, subject);\r\n\r\n    foreach (MessageThread child in thread.Children)\r\n    {\r\n        ShowThread(level + 1, child, envelopes);\r\n    }\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nUsing imap As New Imap()\r\n\timap.UseBestLogin(&quot;imap.example.org&quot;)\t' or ConnectSSL\r\n\timap.Login(&quot;user&quot;, &quot;password&quot;)\r\n\r\n\tDim method As ThreadMethod = ThreadMethod.ChooseBest(imap.SupportedThreadMethods())\r\n\tIf method Is Nothing Then\r\n\t\tThrow New Exception(&quot;This server does not support any threading algorithm.&quot;)\r\n\tEnd If\r\n\r\n\timap.SelectInbox()\r\n\r\n\tDim threads As List(Of MessageThread) = _ \r\n\t\timap.Thread(method).Where(Expression.All())\r\n\tDim envelopes As List(Of Envelope) = \r\n\t\t_ imap.GetEnvelopeByUID(imap.Search().Where(Expression.All()))\r\n\r\n\tFor Each thread As MessageThread In threads\r\n\t\tShowThread(0, thread, envelopes)\r\n\tNext\r\n\timap.Close()\r\nEnd Using\r\n\r\nPublic Sub ShowThread(level As Integer, thread As MessageThread, envelopes As List(Of Envelope))\r\n\tDim indent As New String(&quot; &quot;C, level * 4)\r\n\tDim subject As String = envelopes.Find(Function(x) x.UID = thread.UID).Subject\r\n\tConsole.WriteLine(&quot;{0}{1}: {2}&quot;, indent, thread.UID, subject)\r\n\r\n\tFor Each child As MessageThread In thread.Children\r\n\t\tShowThread(level + 1, child, envelopes)\r\n\tNext\r\nEnd Sub\r\n\r\n<\/pre>\n<p>Here you can see the results:<\/p>\n<pre>\r\n2299: First thread\r\n    2300: Re: First thread\r\n    2301: Re: First thread\r\n2302: Second thread\r\n    2303: Re: Second thread\r\n    2304: Re: Re: Second thread\r\n    2305: Re: Re: Re: Second thread\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The THREAD extension to the IMAP protocol provide a means of server-based threading of messages, without requiring that the IMAP component download the necessary data to do so itself. Following sample shows how to use Imap Thread method to thread all emails available in the mailbox. First it checks if THREAD extension is available on [&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-3215","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\/3215"}],"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=3215"}],"version-history":[{"count":14,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/3215\/revisions"}],"predecessor-version":[{"id":4958,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/3215\/revisions\/4958"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=3215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=3215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=3215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}