{"id":379,"date":"2010-01-04T18:59:13","date_gmt":"2010-01-04T16:59:13","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=379"},"modified":"2014-08-16T12:08:46","modified_gmt":"2014-08-16T10:08:46","slug":"uploading-emails-using-imap","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/uploading-emails-using-imap","title":{"rendered":"Uploading emails using IMAP"},"content":{"rendered":"<p>Uploading emails in .NET to the IMAP server is fairly easy with Mail.dll <a href=\"\/mail\">IMAP library for .NET<\/a><\/p>\n<h2>Uploading new email to IMAP<\/h2>\n<p>First sample shows how to create <strong>new email message<\/strong> and upload it to specified IMAP folder. As usual we&#8217;ll be using <em>MailBuilder<\/em> class to create new email. It&#8217;s going to be a simple text message from Alice to Bob:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMailBuilder builder = new MailBuilder();\r\nbuilder.Subject = &quot;subject&quot;;\r\nbuilder.From.Add(new MailBox(&quot;alice@email.com&quot;, &quot;Alice&quot;));\r\nbuilder.To.Add(new MailBox(&quot;bob@email.com&quot;, &quot;Bob&quot;));\r\nbuilder.Text = &quot;This is plain text email&quot;;\r\nIMail email = builder.Create();\r\n<\/pre>\n<p>Next we&#8217;ll connect to IMAP server and upload this email. Of course you can <a href=\"\/blog\/send-email\">send this email<\/a> before uploading.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing (Imap imap = new Imap())\r\n{\r\n    imap.Connect(&quot;imap.example.com&quot;);    \/\/ or ConnectSSL for SSL\r\n    imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n\r\n    \/\/ The name of the folder depends on your IMAP server\r\n    imap.UploadMessage(&quot;&#x5B;Gmail]\/Sent Mail&quot;, email);\r\n\r\n    imap.Close();\r\n}\r\n<\/pre>\n<p>You may choose any folder for your upload. However in the most common scenario, you&#8217;ll want to upload message to the Sent folder. If your server supports XLIST, SPECIAL-USE extension or at least follow common naming conventions <em>CommonFolders<\/em> may help you:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nCommonFolders folders = new CommonFolders(imap.GetFolders());\r\nimap.UploadMessage(folders.Sent, email);\r\n<\/pre>\n<p>Here you can find more details on <a href=\"\/blog\/common-imap-folders\">obtaining common IMAP folders<\/a><\/p>\n<p>Here&#8217;s the VB .NET version of the above code:<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET code\r\n\r\n' Create new mail message\r\nDim builder As New MailBuilder()\r\nbuilder.Subject = &quot;subject&quot;\r\nbuilder.From.Add(New MailBox(&quot;alice@email.com&quot;, &quot;Alice&quot;))\r\nbuilder.&#x5B;To].Add(New MailBox(&quot;bob@email.com&quot;, &quot;Bob&quot;))\r\nbuilder.Text = &quot;This is plain text email&quot;\r\n\r\nDim email As IMail = builder.Create()\r\n\r\nUsing imap As New Imap()\r\n\timap.Connect(&quot;&quot;imap.example.com&quot;)    ' or ConnectSSL for SSL\r\n\timap.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\r\n\t' The name of the folder depends on your IMAP server\r\n\timap.UploadMessage(&quot;&#x5B;Gmail]\/Sent Mail&quot;, email)\r\n\r\n\timap.Close()\r\nEnd Using\r\n<\/pre>\n<h2>Uploading existing email to IMAP<\/h2>\n<p>In the second example we&#8217;ll upload an existing email in eml format from disk to the IMAP server in .NET.<\/p>\n<p>*.eml extension is a standard extension used for storing emails. Eml file contains raw data received from IMAP or POP3 server. You can use <em>GetMessageByUID<\/em> on <em>Pop3 <\/em>or <em>Imap <\/em>class to obtain those data. It can be also created using <em>IMail.Render<\/em> method.<\/p>\n<p>Eml file includes, email message body in plain text, HTML (if defined) formats, all email headers (such as: from, to, subject, date and so on), and all visual elements and all attachments.<\/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;server&quot;);  \/\/ or ConnectSSL for SSL\r\n    imap.Login(&quot;user&quot;, &quot;password&quot;);\r\n\r\n    byte&#x5B;] eml = File.ReadAllBytes(&quot;email.eml&quot;);\r\n\r\n    \/\/ The name of the folder depends on your IMAP server\r\n    imap.UploadMessage(&quot;&#x5B;Gmail]\/Sent Mail&quot;, 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;server&quot;)  ' or ConnectSSL for SSL\r\n\timap.Login(&quot;user&quot;, &quot;password&quot;)\r\n\r\n\tDim eml As Byte() = File.ReadAllBytes(&quot;email.eml&quot;)\r\n\r\n\t' The name of the folder depends on your IMAP server\r\n\timap.UploadMessage(&quot;&#x5B;Gmail]\/Sent Mail&quot;, eml)\r\n\r\n\timap.Close()\r\nEnd Using\r\n<\/pre>\n<p>Please note that only few IMAP servers are going to send the message to the actual recipients when it is uploaded. Most servers will only store the message without sending it. You should use SMTP protocol <a href=\"\/blog\/send-email\">to send email<\/a> before uploading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Uploading emails in .NET to the IMAP server is fairly easy with Mail.dll IMAP library for .NET Uploading new email to IMAP First sample shows how to create new email message and upload it to specified IMAP folder. As usual we&#8217;ll be using MailBuilder class to create new email. It&#8217;s going to be a simple [&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,25,28,77,57],"class_list":["post-379","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-gmail","tag-imap","tag-imap-component","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/379"}],"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=379"}],"version-history":[{"count":10,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/379\/revisions"}],"predecessor-version":[{"id":4770,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/379\/revisions\/4770"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}