{"id":984,"date":"2010-07-08T18:26:35","date_gmt":"2010-07-08T16:26:35","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=984"},"modified":"2013-12-24T12:33:43","modified_gmt":"2013-12-24T10:33:43","slug":"download-parts-of-email-message","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/download-parts-of-email-message","title":{"rendered":"Download parts of email message"},"content":{"rendered":"<p>Sometimes you know you&#8217;ll receive large size emails, but <strong>you only need to access some email parts, without downloading entire email messages<\/strong> from IMAP server.<\/p>\n<p>Mail.dll <a href=\"\/mail\">.NET IMAP library<\/a> allows you to download only needed parts of the specified message.<\/p>\n<p>First you need to download <strong>structure <\/strong>of the email message. There are two methods for that: <em>GetBodyStructureByUID <\/em>and <em>GetBodyStructureByNumber<\/em>.<\/p>\n<p>You can also use <a href=\"\/blog\/get-email-information-from-imap-fast\"><em>GetMessageInfoByUID <\/em>or <em>GetMessageInfoByNumber<\/em><\/a> methods. Both methods return more information about most common email fields, such as: subject, from, to and other headers, and also include <em>BodyStructure <\/em> in their response.<\/p>\n<p><em>BodyStructure<\/em> class contains information about plain text, html, and all attachments that were added to the message. It does not contain any data though. That&#8217;s the reason why, downloading it is very fast.<\/p>\n<p>To <strong>download text<\/strong> parts of the email (like HTML or plain text) you can use: <em>GetTextByUID<\/em> or <em>GetTextByNumber<\/em> methods of <em>Imap<\/em> class.<\/p>\n<p>To <strong>download binary attachments<\/strong> use <em>GetDataByUID<\/em> or <em>GetDataByNumber<\/em> methods of <em>Imap<\/em> class.<\/p>\n<p>Here&#8217;s the full sample for this feature:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing(Imap imap = new Imap())\r\n{\r\n\timap.ConnectSSL(&quot;imap.server.com&quot;);\r\n\timap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n\r\n\timap.SelectInbox();\r\n\tList&lt;long&gt; uidList = imap.Search(Flag.Unseen);\r\n\tforeach (long uid in uidList)\r\n\t{\r\n\t\t\/\/ Get the structure of the email\r\n\t\tBodyStructure structure = imap.GetBodyStructureByUID(uid);\r\n\r\n\t\t\/\/ Download only text and html parts\r\n\t\tstring text, html;\r\n\r\n\t\tif (structure.Text != null)\r\n\t\t    text = imap.GetTextByUID(structure.Text);\r\n\t\tif (structure.Html != null)\r\n\t\t    html = imap.GetTextByUID(structure.Html);\r\n\r\n\t\tConsole.WriteLine(text);\r\n\t\tConsole.WriteLine(html);\r\n\r\n\t\t\/\/ Show all attachments' names\r\n\t\tforeach(MimeStructure attachment in structure.Attachments)\r\n\t\t{\r\n\t\t\tConsole.WriteLine(attachment.SafeFileName);\r\n\t\t\t\/\/ You can also download entire attachment\r\n\t\t\tbyte&#x5B;] bytes = imap.GetDataByUID(attachment);\r\n\t\t}\r\n\t}\r\n\timap.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.ConnectSSL(&quot;imap.server.com&quot;)\r\n\timap.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\r\n\timap.SelectInbox()\r\n\tDim uidList As List(Of Long) = imap.Search(Flag.Unseen)\r\n\tFor Each uid As Long In uidList\r\n\t\t' Get the structure of the email\r\n\t\tDim struct As BodyStructure = imap.GetBodyStructureByUID(uid)\r\n\r\n\t\t' Download only text and html parts\r\n\t\tDim text As String, html As String\r\n\r\n\t\tIf &#x5B;structure].Text IsNot Nothing Then\r\n\t\t    text = imap.GetTextByUID(&#x5B;structure].Text)\r\n\t\tEnd If\r\n\t\tIf &#x5B;structure].Html IsNot Nothing Then\r\n\t\t    html = imap.GetTextByUID(&#x5B;structure].Html)\r\n\t\tEnd If\r\n\r\n\t\tConsole.WriteLine(text)\r\n\t\tConsole.WriteLine(html)\r\n\r\n\t\t' Show all attachments' names\r\n\t\tFor Each attachment As MimeStructure In struct.Attachments\r\n\t\t\tConsole.WriteLine(attachment.SafeFileName)\r\n\t\t\t' You can also download entire attachment\r\n\t\t\tDim bytes As Byte() = imap.GetDataByUID(attachment)\r\n\t\tNext\r\n\tNext\r\n\timap.Close()\r\nEnd Using\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you know you&#8217;ll receive large size emails, but you only need to access some email parts, without downloading entire email messages from IMAP server. Mail.dll .NET IMAP library allows you to download only needed parts of the specified message. First you need to download structure of the email message. There are two methods for [&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":[74,15,28,77,57],"class_list":["post-984","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-attachments","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\/984"}],"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=984"}],"version-history":[{"count":14,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/984\/revisions"}],"predecessor-version":[{"id":4426,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/984\/revisions\/4426"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}