{"id":258,"date":"2010-01-25T18:45:15","date_gmt":"2010-01-25T16:45:15","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=258"},"modified":"2016-09-08T23:01:41","modified_gmt":"2016-09-08T21:01:41","slug":"download-email-attachments-net","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/download-email-attachments-net","title":{"rendered":"Download email attachments in .NET"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2009\/12\/mail.png\" alt=\"\" title=\"mail\" width=\"157\" height=\"149\" class=\"alignleft size-full wp-image-360\" \/>.NET framework does not contain classes that allow access to email servers (SmtpClient can only send messages). Having this in mind, the first thing you&#8217;ll need is an <a href=\"\/mail\">.NET IMAP component<\/a> or <a href=\"\/mail\">.NET POP3 component<\/a> to download emails from the server.<\/p>\n<p>IMAP and POP3 are protocols that allow communication with email servers, like Exchange or Gmail, and download email messages. IMAP is more robust, as it allows searching and grouping emails into folders. You can see <a href=\"\/blog\/pop3-vs-imap\">IMAP vs POP3 comparision here<\/a>.<\/p>\n<p>The <strong>email attachments are downloaded along with the email message<\/strong>. Attachments are stored within the email as part of a mime tree. Usually Quoted-Printable or Base64 encoding is used. This is why apart of an IMAP\/POP3 client, MIME parser is needed. Mail.dll is going to parse such MIME tree for you and expose all attachments as well-known .NET collections. Of course all other email properties, like subject, date, recipients and body, are also available.<\/p>\n<p>IMail (class that represents an email after it was downloaded and parsed) uses 4 collections for storing attachments:<\/p>\n<ul>\n<li><em><strong>IMail.Attachments<\/strong><\/em> &#8211; all attachments (includes Visuals, NonVisuals and Alternatives).<\/li>\n<li><em>IMail.Visuals<\/em> &#8211; visual elements, files that should be displayed to the user, such as images embedded in an HTML email.<\/li>\n<li><em>IMail.NonVisuals<\/em> &#8211; non visual elements, &#8220;real&#8221; attachments.<\/li>\n<li><em>IMail.Alternatives<\/em> &#8211; alternative content representations, for example ical appointment.<\/li>\n<\/ul>\n<p>Below you&#8217;ll find samples of how you can save all attachments to disk using C# and VB.NET via POP3 and IMAP protocols.<\/p>\n<h2>Download attachments from IMAP server<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing(Imap imap = new Imap())\r\n{\r\n\timap.Connect(&quot;imap.example.com&quot;);   \/\/ or ConnectSSL for SSL\r\n\timap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n\r\n\timap.SelectInbox();\r\n\tList&lt;long&gt; uids = imap.Search(Flag.All);\r\n\r\n\tforeach (long uid in uids)\r\n\t{\r\n\t\tIMail email = new MailBuilder()\r\n\t\t\t.CreateFromEml(imap.GetMessageByUID(uid));\r\n\r\n\t\tConsole.WriteLine(email.Subject);\r\n\r\n\t\t\/\/ save all attachments to disk\r\n\t\tforeach(MimeData mime in email.Attachments)\r\n\t\t{\r\n\t\t\tmime.Save(mime.SafeFileName);\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.Connect(&quot;imap.example.com&quot;)   ' or ConnectSSL for SSL\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.All)\r\n\r\n\tFor Each uid As Long In uids\r\n\t\tDim email As IMail = New MailBuilder()_\r\n\t\t\t.CreateFromEml(imap.GetMessageByUID(uid))\r\n\r\n\t\tConsole.WriteLine(email.Subject)\r\n\r\n\t\t' save all attachments to disk\r\n\t\tFor Each mime As MimeData In email.Attachments\r\n\t\t\tmime.Save(mime.SafeFileName)\r\n\t\tNext\r\n\tNext\r\n\timap.Close()\r\nEnd Using\r\n<\/pre>\n<h2>Download attachments from POP3 server<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# \r\n\r\nusing(Pop3 pop3 = new Pop3())\r\n{\r\n\tpop3.Connect(&quot;pop3.example.com&quot;);   \/\/ or ConnectSSL\r\n\tpop3.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n\r\n\tforeach (string uid in pop3.GetAll())\r\n\t{\r\n\t\tIMail email = new MailBuilder()\r\n\t\t\t.CreateFromEml(pop3.GetMessageByUID(uid));\r\n\r\n\t\tConsole.WriteLine(email.Subject);\r\n\r\n\t\t\/\/ save all attachments to disk\r\n\t\tforeach(MimeData mime in email.Attachments)\r\n\t\t{\r\n\t\t\tmime.Save(mime.SafeFileName);\r\n\t\t}\r\n\t}\r\n\tpop3.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nUsing pop3 As New Pop3()\r\n\tpop3.Connect(&quot;pop3.example.com&quot;) ' or ConnectSSL\r\n\tpop3.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\r\n\tFor Each uid As String In pop3.GetAll()\r\n\t\tDim email As IMail = New MailBuilder() _\r\n      \t\t\t.CreateFromEml(pop3.GetMessageByUID(uid))\r\n\r\n\t\tConsole.WriteLine(email.Subject)\r\n\r\n\t\t' save all attachments to disk\r\n\t\tFor Each mime As MimeData In email.Attachments\r\n\t\t\tmime.Save(mime.SafeFileName)\r\n\t\tNext\r\n\tNext\r\n\tpop3.Close()\r\nEnd Using\r\n<\/pre>\n<h2>Accessing attachment&#8217;s data<\/h2>\n<p>You can also save attachment to stream (using <em>MimeData.Save(Stream stream)<\/em> method), get direct access to attachments data as stream (using <em>MemoryStream MimeData.GetMemoryStream()<\/em> method) or even as a byte array (using <em>byte[] MimeData.Data<\/em>).<\/p>\n<h2>Process emails embedded as attachments<\/h2>\n<p>In many situations you&#8217;ll receive a message that has another message attached to it. You can use Mail.dll to <a href=\"\/blog\/process-emails-embedded-as-attachments\">extract all attachments from all inner messages<\/a> no matter on how deep the embedding level is.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>.NET framework does not contain classes that allow access to email servers (SmtpClient can only send messages). Having this in mind, the first thing you&#8217;ll need is an .NET IMAP component or .NET POP3 component to download emails from the server. IMAP and POP3 are protocols that allow communication with email servers, like Exchange or [&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,33,28,42,57],"class_list":["post-258","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-attachments","tag-c","tag-email-component","tag-imap","tag-pop3","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/258"}],"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=258"}],"version-history":[{"count":11,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/258\/revisions"}],"predecessor-version":[{"id":5043,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/258\/revisions\/5043"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}