{"id":1140,"date":"2010-09-16T18:42:09","date_gmt":"2010-09-16T16:42:09","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1140"},"modified":"2014-07-29T11:42:04","modified_gmt":"2014-07-29T09:42:04","slug":"get-email-information-from-imap-fast","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/get-email-information-from-imap-fast","title":{"rendered":"Get email information from IMAP (fast)"},"content":{"rendered":"<p>Email messages include attachments (encoded as text using Base64 or Quoted-Printable encoding). This causes emails to became quite big and download process slow. In many cases you don&#8217;t require to download entire message (including attachments) to process it. Think about displaying only a list of messages that are in the INBOX folder or only deciding if you should process a message.<\/p>\n<p>When using IMAP protocol, Mail.dll <a href=\"\/mail\">IMAP client<\/a> offers <strong> extremely fast way of downloading such email information<\/strong>. This include message envelope information (Subject, Date, From and To recipients) and message structure (Attachment count, attachment names and sizes).<\/p>\n<p>In the following sample, we&#8217;ll download those data for all unseen messages that are in the INBOX folder:<\/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;);   \/\/ or ConnectSSL\r\n    imap.UseBestLogin(&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    List&lt;MessageInfo&gt; infos = imap.GetMessageInfoByUID(uids);\r\n\r\n    foreach (MessageInfo info in infos)\r\n    {\r\n        Console.WriteLine(&quot;Subject: &quot; + info.Envelope.Subject);\r\n        Console.WriteLine(&quot;From: &quot; + info.Envelope.From);\r\n        Console.WriteLine(&quot;To: &quot; + info.Envelope.To);\r\n        foreach (MimeStructure attachment in info.BodyStructure.Attachments)\r\n        {\r\n            Console.WriteLine(&quot;  Attachment: '{0}' ({1} bytes)&quot;,\r\n                attachment.SafeFileName,\r\n                attachment.Size);\r\n        }\r\n        Console.WriteLine();\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\nUsing imap As New Imap()\r\n\timap.Connect(&quot;imap.example.com&quot;)    ' or ConnectSSL\r\n\timap.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\r\n\timap.SelectInbox()\r\n\tDim uids As List(Of Long) = imap.Search(Flag.Unseen)\r\n\tDim infos As List(Of MessageInfo) = imap.GetMessageInfoByUID(uids)\r\n\r\n\tFor Each info As MessageInfo In infos\r\n\t\tConsole.WriteLine(&quot;Subject: &quot; + info.Envelope.Subject)\r\n\t\tConsole.WriteLine(&quot;From: &quot; + info.Envelope.From)\r\n\t\tConsole.WriteLine(&quot;To: &quot; + info.Envelope.&#x5B;To])\r\n\t\tFor Each attachment As MimeStructure In info.BodyStructure.Attachments\r\n\t\t\tConsole.WriteLine(&quot;  Attachment: '{0}' ({1} bytes)&quot;, _\r\n\t\t\t\tattachment.SafeFileName,  _\r\n\t\t\t\tattachment.Size)\r\n\t\tNext\r\n\t\tConsole.WriteLine()\r\n\tNext\r\n\timap.Close()\r\nEnd Using\r\n<\/pre>\n<p>The result of the code above is list of all unread emails with attachments:<\/p>\n<p><code><br \/>\nSubject: Contract<br \/>\nFrom: Alice &lt;alice@company.com&gt;<br \/>\nTo: Bob &lt;bob@company.com&gt;<br \/>\n  Attachment: 'document1.doc' (42738 bytes)<br \/>\n  Attachment: '.NET.pdf' (1243 bytes)<br \/>\n<\/code><\/p>\n<p><strong>Envelope <\/strong>object contains following information:<\/p>\n<ul>\n<li><strong>Subject<\/strong><\/li>\n<li><strong>Date<\/strong><\/li>\n<li><strong>From<\/strong><\/li>\n<li>Sender<\/li>\n<li>To<\/li>\n<li>Cc<\/li>\n<li>InReplyTo<\/li>\n<li>Message id<\/li>\n<li>Message size<\/li>\n<li>Gmail thread id (if available)<\/li>\n<li>Gmail message id (if available)<\/li>\n<\/ul>\n<p><strong>BodyStructure <\/strong>contains information about:<\/p>\n<ul>\n<li>All attachments including their size, name, content type<\/li>\n<li>HTML message part<\/li>\n<li>Pain text message part<\/li>\n<\/ul>\n<p>It&#8217;s really fast. <strong>Downloading 1000 email<\/strong> information takes approximately <strong>13 seconds<\/strong>.<\/p>\n<p>You can read more about <a href=\"\/blog\/download-parts-of-email-message\">downloading email parts (single attachment) here<\/a><\/p>\n<p>You can <a href=\"\/mail\">download Mail.dll .NET IMAP library here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Email messages include attachments (encoded as text using Base64 or Quoted-Printable encoding). This causes emails to became quite big and download process slow. In many cases you don&#8217;t require to download entire message (including attachments) to process it. Think about displaying only a list of messages that are in the INBOX folder or only deciding [&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-1140","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\/1140"}],"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=1140"}],"version-history":[{"count":9,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1140\/revisions"}],"predecessor-version":[{"id":4748,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1140\/revisions\/4748"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}