{"id":1264,"date":"2010-10-25T15:31:06","date_gmt":"2010-10-25T13:31:06","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1264"},"modified":"2014-04-15T17:24:13","modified_gmt":"2014-04-15T15:24:13","slug":"save-all-attachments-to-disk-using-pop3","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/save-all-attachments-to-disk-using-pop3","title":{"rendered":"Save all attachments to disk using POP3"},"content":{"rendered":"<p>This article describes how to save all email messages&#8217; attachments to disk. The messages are downloaded using Mail.dll library and POP3 protocol.<\/p>\n<p>The first thing you need to know is that<strong> email attachments are downloaded along with the email message<\/strong>. This means that invoking <em>GetMessageByUID<\/em> method is going to download entire email message including all attachments.<\/p>\n<p>Mail.dll exposes all attachments as well-known .NET collections. There are 4 collections that may contain 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>You should use <em>IMail.Attachments<\/em> collection to get all attachments. Every attachment is represented by <em>MimeData<\/em> object.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing Limilabs.Mail;\r\nusing Limilabs.Mail.MIME;\r\nusing Limilabs.Client.POP3;\r\n\r\nclass Program\r\n{\r\n    static void Main(string&#x5B;] args)\r\n    {\r\n        Pop3 pop3 = new Pop3();\r\n        pop3.Connect(&quot;pop3.example.com&quot;);\r\n        pop3.Login(&quot;user&quot;, &quot;password&quot;);\r\n\r\n        foreach (string uid in pop3.GetAll())\r\n        {\r\n            var eml = pop3.GetMessageByUID(uid);\r\n            IMail email = new MailBuilder()\r\n                .CreateFromEml(eml);\r\n\r\n            Console.WriteLine(email.Subject);\r\n\r\n            foreach (MimeData mime in email.Attachments)\r\n            {\r\n                mime.Save(@&quot;c:\\&quot; + mime.SafeFileName);\r\n            }\r\n        }\r\n\r\n        pop3.Close();\r\n    }\r\n};\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nImports Limilabs.Mail\r\nImports Limilabs.Mail.MIME\r\nImports Limilabs.Client.POP3\r\n\r\nPublic Module Module1\r\n    Public Sub Main(ByVal args As String())\r\n\r\n        Using pop3 As New Pop3()\r\n            pop3.Connect(&quot;pop3.example.com&quot;)\r\n            pop3.Login(&quot;user&quot;, &quot;password&quot;)\r\n\r\n            Dim uids As List(Of Long) = pop3.GetAll()\r\n\r\n            For Each uid As Long In uids\r\n                Dim eml = pop3.GetMessageByUID(uid)\r\n                Dim email As IMail = New MailBuilder() _\r\n                    .CreateFromEml(eml)\r\n                Console.WriteLine(email.Subject)\r\n\r\n                For Each mime As MimeData In email.Attachments\r\n                    mime.Save(&quot;c:\\&quot; + mime.SafeFileName)\r\n                Next\r\n            Next\r\n            pop3.Close()\r\n        End Using\r\n\r\n    End Sub\r\nEnd Module\r\n\r\n<\/pre>\n<h2>Accessing attachment&#8217;s data<\/h2>\n<p>You can also save attachment to stream: <em>void MimeData.Save(Stream stream)<\/em>.<br \/>\nYou can <strong>directly access attachment&#8217;s data<\/strong> as stream using <em>MimeData.GetMemoryStream()<\/em> method or as byte array using <em>MimeData.Data<\/em> property.<\/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. You can use Mail.dll to <a href=\"\/blog\/process-emails-embedded-as-attachments\">extract all attachments from such inner messages<\/a> no matter on how deep the embedding level is.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article describes how to save all email messages&#8217; attachments to disk. The messages are downloaded using Mail.dll library and POP3 protocol. The first thing you need to know is that email attachments are downloaded along with the email message. This means that invoking GetMessageByUID method is going to download entire email message including all [&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,42,57],"class_list":["post-1264","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-attachments","tag-c","tag-email-component","tag-pop3","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1264"}],"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=1264"}],"version-history":[{"count":9,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1264\/revisions"}],"predecessor-version":[{"id":2689,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1264\/revisions\/2689"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}