{"id":1191,"date":"2011-01-11T13:41:05","date_gmt":"2011-01-11T11:41:05","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1191"},"modified":"2014-04-15T14:56:34","modified_gmt":"2014-04-15T12:56:34","slug":"process-emails-embedded-as-attachments","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/process-emails-embedded-as-attachments","title":{"rendered":"Process emails embedded as attachments"},"content":{"rendered":"<p>This article describes <strong>how to process emails embedded within emails as attachments<\/strong>. Mail.dll supports opening and extraction of embedded emails and attachments within.<\/p>\n<h2>Extracting all attachments<\/h2>\n<p>There is an easy way out &#8211; using <em>ExtractAttachmentsFromInnerMessages<\/em> method. It <strong>extracts all attachments from the email and from all attached messages<\/strong>. It returns easy to use collection of <em>MimeData<\/em> objects that represent each attachment.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing (Imap client = new Imap())\r\n{\r\n    client.ConnectSSL(&quot;imap.example.com&quot;);\r\n    client.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n    client.SelectInbox();\r\n    foreach (long uid in client.GetAll())\r\n    {\r\n        IMail mail = new MailBuilder().CreateFromEml(\r\n            client.GetMessageByUID(uid));\r\n\r\n        ReadOnlyCollection&lt;MimeData&gt; attachments = \r\n            mail.ExtractAttachmentsFromInnerMessages();\r\n      \r\n        foreach (MimeData mime in attachments)\r\n        {\r\n            mime.Save(@&quot;c:\\&quot; + mime.SafeFileName);\r\n        }\r\n    }\r\n    client.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET version\r\n\r\nUsing client As New Imap()\r\n    client.ConnectSSL(&quot;imap.example.com&quot;)\r\n    client.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n    client.SelectInbox()\r\n    For Each uid As Long In client.GetAll()\r\n        Dim mail As IMail = New MailBuilder() _\r\n            .CreateFromEml(client.GetMessageByUID(uid))\r\n\r\n        Dim attachments As ReadOnlyCollection(Of MimeData) _\r\n            = mail.ExtractAttachmentsFromInnerMessages()\r\n\r\n        For Each mime As MimeData In attachments\r\n            mime.Save(&quot;c:\\&quot; + mime.SafeFileName)\r\n        Next\r\n           \r\n    Next\r\n    client.Close()\r\nEnd Using\r\n<\/pre>\n<h2>Manual approach<\/h2>\n<p>This gives you more control of which attachments to process, and which for some reason you wisch to ignore. We&#8217;ll create a simple <strong>method that processes all attached emails and extracts all attachments<\/strong> to specified folder.<\/p>\n<p>First we&#8217;ll use Mail.dll IMAP component to download emails:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing (Imap client = new Imap())\r\n{\r\n    client.ConnectSSL(&quot;imap.example.com&quot;);\r\n    client.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n    client.SelectInbox();\r\n    foreach (long uid in client.GetAll())\r\n    {\r\n        IMail mail = new MailBuilder().CreateFromEml(\r\n            client.GetMessageByUID(uid));\r\n\r\n        SaveAttachmentsTo(mail, @&quot;c:\\tmp&quot;);\r\n    }\r\n    client.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET version\r\n\r\nUsing client As New Imap()\r\n    client.ConnectSSL(&quot;imap.example.com&quot;)\r\n    client.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n    client.SelectInbox()\r\n    For Each uid As Long In client.GetAll()\r\n        Dim mail As IMail = New MailBuilder() _\r\n            .CreateFromEml(client.GetMessageByUID(uid))\r\n\r\n        SaveAttachmentsTo(mail, @&quot;c:\\tmp&quot;)\r\n    Next\r\n    client.Close()\r\nEnd Using\r\n<\/pre>\n<p>The following method traverses through all attachments. If regular attachment is found it is saved to specified folder. If <strong>email message is attached<\/strong> it <strong>parses it<\/strong> and <strong>saves all its attachments using recursion<\/strong>:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nprivate void SaveAttachmentsTo(IMail mail, string folder)\r\n{\r\n    foreach (MimeData attachment in mail.Attachments)\r\n    {\r\n        if (attachment is IMailContainer)\r\n        {\r\n            IMail attachedMessage = ((IMailContainer) attachment).Message;\r\n            SaveAttachmentsTo(attachedMessage, folder);\r\n        }\r\n        else\r\n        {\r\n            attachment.Save(\r\n                Path.Combine(folder, attachment.SafeFileName));\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET version\r\n\r\nPrivate Sub SaveAttachmentsTo(mail As IMail, folder As String)\r\n    For Each attachment As MimeData In mail.Attachments\r\n        If TypeOf attachment Is IMailContainer Then\r\n            Dim attachedMessage As IMail = DirectCast(attachment, IMailContainer).Message\r\n    \t    SaveAttachmentsTo(attachedMessage, folder)\r\n        Else\r\n            attachment.Save( _\r\n                Path.Combine(folder, attachment.SafeFileName))\r\n        End If\r\n    Next\r\nEnd Sub\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This article describes how to process emails embedded within emails as attachments. Mail.dll supports opening and extraction of embedded emails and attachments within. Extracting all attachments There is an easy way out &#8211; using ExtractAttachmentsFromInnerMessages method. It extracts all attachments from the email and from all attached messages. It returns easy to use collection of [&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,20,28,57],"class_list":["post-1191","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-attachments","tag-c","tag-email-component","tag-eml","tag-imap","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1191"}],"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=1191"}],"version-history":[{"count":10,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1191\/revisions"}],"predecessor-version":[{"id":2696,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1191\/revisions\/2696"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}