{"id":1262,"date":"2010-10-25T15:30:30","date_gmt":"2010-10-25T13:30:30","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1262"},"modified":"2014-04-15T17:25:16","modified_gmt":"2014-04-15T15:25:16","slug":"save-images-embedded-in-html-email-to-disk-using-pop3","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/save-images-embedded-in-html-email-to-disk-using-pop3","title":{"rendered":"Save images embedded in HTML email to disk using POP3"},"content":{"rendered":"<p>This article shows how to save all images embedded in HTML email message to disk. The messages are downloaded using Mail.dll <a href=\"\/mail\">.NET POP3 component<\/a> and POP3 protocol.<\/p>\n<p>In most cases HTML body of the message references such images using special &#8220;cid:&#8221; protocol, that specifies <strong>Content-ID<\/strong> of the image that should be used:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nThis is our &lt;strong&gt;brand new&lt;\/strong&gt; logo: &lt;br \/&gt;\r\n&lt;img src=&quot;cid:logo@example.com&quot; \/&gt;\r\n<\/pre>\n<p>In such case actual image is embedded inside an email, as part of the mime tree, as an element <strong>related<\/strong> to HTML body and with content-disposition header set to <strong>inline<\/strong>. This means that invoking <em>GetMessageByUID <\/em>method is going to download entire email message, including all images. This makes message bigger, but images don&#8217;t need to be stored on your web server (and remember that emails tend to be archived for years). <\/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>As you can see the most interesting from this article&#8217;s point of view is <em>IMail.Visuals<\/em> collection. As with regular attachments, visual elements are  represented by MimeData objects.<\/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;server.company.com&quot;);\r\n        pop3.Login(&quot;user&quot;, &quot;password&quot;);\r\n\r\n        \/\/ Receive all messages\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.Visuals)\r\n            {\r\n                mime.Save(@&quot;c:\\&quot; + mime.SafeFileName);\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;server.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.Visuals\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<\/pre>\n<p>You can also save attachment to stream <em>MimeData.Save(Stream stream)<\/em>, get direct access to it as stream <em>MemoryStream MimeData.GetMemoryStream()<\/em><br \/>\nor as byte array using <em>byte[] MimeData.Data<\/em> property.<\/p>\n<p>You can also use <em>SaveHtmlAs<\/em> method to <strong>save entire message as regular HTML page<\/strong> with all required images and styles to a single folder:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nIMail email = ...\r\nemail.SaveHtmlAs(@&quot;c:\\tmp\\email.html&quot;);\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim email As IMail = ...\r\nemail.SaveHtmlAs(&quot;c:\\tmp\\email.html&quot;)\r\n<\/pre>\n<p>What is also worth mentioning is the fact that you can use Content-Id to search through <em>IMail.Visuals<\/em> collection:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nIMail email = ...;\r\nMimeData logo = email.Visuals&#x5B;&quot;logo@example.com&quot;];\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n\/\/ VB.NET\r\n\r\nDim email As IMail = ...\r\nDim logo As MimeData = email.Visuals(&quot;logo@example.com&quot;)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to save all images embedded in HTML email message to disk. The messages are downloaded using Mail.dll .NET POP3 component and POP3 protocol. In most cases HTML body of the message references such images using special &#8220;cid:&#8221; protocol, that specifies Content-ID of the image that should be used: This is our [&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,33,42],"class_list":["post-1262","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-email-component","tag-pop3"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1262"}],"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=1262"}],"version-history":[{"count":7,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1262\/revisions"}],"predecessor-version":[{"id":3095,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1262\/revisions\/3095"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}