{"id":344,"date":"2011-12-07T16:03:55","date_gmt":"2011-12-07T14:03:55","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=344"},"modified":"2015-02-26T20:04:52","modified_gmt":"2015-02-26T18:04:52","slug":"save-raw-eml-file-imap-pop3","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/save-raw-eml-file-imap-pop3","title":{"rendered":"Save raw eml file using IMAP and POP3"},"content":{"rendered":"<p>There are times that you need to save <strong>raw email data in eml format<\/strong>.<\/p>\n<p>This tutorial shows how to use <a href=\"\/mail\">Mail.dll POP3 and IMAP clients <\/a> to achieve this goal.<\/p>\n<p>Note that we don&#8217;t need to parse the emails, so we aren&#8217;t using <em>MailBuilder<\/em> class and <em>IMail<\/em> interface.<\/p>\n<p>Samples provided use both POP3 and IMAP protocols and both C# and VB.NET.<\/p>\n<h2>Save email using IMAP:<\/h2>\n<p>The code below will search for all the emails with specified subject and write all these emails to your c: drive.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# code\r\n\r\nusing (Imap imap = new Imap())\r\n{\r\n    imap.Connect(&quot;imap.example.com&quot;);    \/\/ use ConnectSSL for SSL connection.\r\n    imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n\r\n    imap.SelectInbox();\r\n\r\n    List&lt;long&gt; uids = imap.Search(\r\n        Expression.Subject(&quot;email subject&quot;));\r\n\r\n    foreach (long uid in uids)\r\n    {\r\n        var eml = imap.GetMessageByUID(uid);\r\n        string fileName = string.Format(@&quot;c:\\email_{0}.eml&quot;, uid);\r\n        File.WriteAllBytes(fileName, eml);\r\n    }\r\n    imap.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET code\r\n\r\nUsing imap As New Imap()\r\n\timap.Connect(&quot;imap.example.com&quot;)    ' use ConnectSSL for SSL connection.\r\n\timap.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\r\n\timap.SelectInbox()\r\n\r\n\tDim uids As List(Of Long) = imap.Search( _\r\n\t\tExpression.Subject(&quot;email subject&quot;))\r\n\r\n\tFor Each uid As Long In uids\r\n\t\tDim eml = imap.GetMessageByUID(uid)\r\n\t\tDim fileName As String = String.Format(@&quot;c:\\email_{0}.eml&quot;, uid)\r\n\t\tFile.WriteAllBytes(fileName, eml)\r\n\tNext\r\n\timap.Close()\r\nEnd Using\r\n<\/pre>\n<h2>Save email using POP3:<\/h2>\n<p>As POP3 protocol does not allow searching, the code below will write all emails to your c: drive. You can easily limit this to the specified uid.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# code\r\n\r\nusing (Pop3 pop3 = new Pop3())\r\n{\r\n    pop3.Connect(&quot;pop3.example.com&quot;);    \/\/ use ConnectSSL for SSL connection.\r\n    pop3.UseBestLogin(&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        string fileName = string.Format(@&quot;c:\\email_{0}.eml&quot;, uid);\r\n        File.WriteAllBytes(fileName, eml);\r\n    }\r\n    pop3.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET code\r\n\r\nUsing pop3 As New Pop3()\r\n\tpop3.Connect(&quot;pop3.example.com&quot;)    'use ConnectSSL for SSL connection.\r\n\tpop3.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\r\n  For Each uid As String In pop3.GetAll()\r\n\t\tDim eml = pop3.GetMessageByUID(uid)\r\n\t\tDim fileName As String = String.Format(&quot;c:\\email_{0}.eml&quot;, uid)\r\n\t\tFile.WriteAllBytes(fileName, eml)\r\n\tNext\r\n\tpop3.Close()\r\nEnd Using\r\n<\/pre>\n<p>You can download Mail.dll at: <a href=\"\/mail\">Mail.dll .NET email component<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are times that you need to save raw email data in eml format. This tutorial shows how to use Mail.dll POP3 and IMAP clients to achieve this goal. Note that we don&#8217;t need to parse the emails, so we aren&#8217;t using MailBuilder class and IMail interface. Samples provided use both POP3 and IMAP protocols [&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,20,28,77,42,79,57],"class_list":["post-344","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-eml","tag-imap","tag-imap-component","tag-pop3","tag-pop3-component","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/344"}],"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=344"}],"version-history":[{"count":13,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/344\/revisions"}],"predecessor-version":[{"id":4913,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/344\/revisions\/4913"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}