{"id":262,"date":"2009-11-27T12:00:33","date_gmt":"2009-11-27T10:00:33","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=262"},"modified":"2023-03-24T13:52:06","modified_gmt":"2023-03-24T11:52:06","slug":"download-emails-from-gmail","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/download-emails-from-gmail","title":{"rendered":"Download emails from Gmail"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"alignleft\"><img loading=\"lazy\" decoding=\"async\" width=\"131\" height=\"61\" src=\"\/blog\/wp-content\/uploads\/2009\/11\/gmail.png\" alt=\"gmail\" class=\"wp-image-271\" title=\"gmail\"\/><\/figure><\/div>\n\n\n\n<p>The task is quite easy with <a href=\"\/mail\">Mail.dll IMAP client<\/a>. IMAP protocol unlike POP3 stores the unseen information on the server. So all we need to do is connect via SSL\/TLS, search for unseen emails, and download them. Mail.dll will perform all the hard work.<\/p>\n\n\n\n<p>First thing to remember is to <a href=\"\/blog\/enable-imap-in-gmail\">enable IMAP in Gmail<\/a> (your authentication options are app-passwords and OAuth 2.0) , second is to use <code>ConnectSSL<\/code> method, as Gmail allows <strong>secure SSL\/TLS<\/strong> connections only.<\/p>\n\n\n\n<p>We&#8217;ll use <code>Imap.Search(Flag.Unseen)<\/code> method to download unique ids of all unseen email messages in our inbox. Finally the sample shows how to download such emails and parse them using <code>MailBuilder<\/code> class. After this it is easy to access any email data like: <code>Subject<\/code>, <code>Date<\/code>, <code>From<\/code> and <code>To<\/code> collections, plain-text and HTML versions of the email. Also all attachments are downloaded and stored in <code>IMail.Attachments<\/code> collection.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C# code:\n\nusing(Imap imap = new Imap())\n{\n\timap.ConnectSSL(&quot;imap.gmail.com&quot;);\n\timap.UseBestLogin(&quot;pat@gmail.com&quot;, &quot;app-password&quot;);\n\n\timap.SelectInbox();\n\n\tLis&lt;long&gt; uids = imap.Search(Flag.Unseen);\n\tforeach (long uid in uids)\n\t{\n\t\tvar eml = imap.GetMessageByUID(uid);\n\t\tIMail mail = new MailBuilder().CreateFromEml(eml);\n\n\t\tConsole.WriteLine(mail.Subject);\n\t\tConsole.WriteLine(mail.Text);\n\t}\n\timap.Close();\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB.NET code:\n\nUsing imap As New Imap()\n\timap.ConnectSSL(\"imap.gmail.com\")\n\timap.UseBestLogin(\"pat@gmail.com\", \"password\")\n\n\timap.SelectInbox()\n\n\tDim uids As List(Of Long) = imap.Search(Flag.Unseen)\n\tFor Each uid As Long In uids\n\t\tDim eml = imap.GetMessageByUID(uid)\n\t\tDim mail As IMail = New MailBuilder().CreateFromEml(eml)\n\n\t\tConsole.WriteLine(mail.Subject)\n\t\tConsole.WriteLine(mail.Text)\n\tNext\n\timap.Close()\nEnd Using\n<\/pre><\/div>\n\n\n<p>If you like the idea of such simple Gmail access, just give it a try and download <a href=\"\/mail\">Mail.dll .NET IMAP component<\/a>.<\/p>\n\n\n\n<p>The next step is to learn <a href=\"\/blog\/save-all-attachments-to-disk-using-imap\">how to process email attachments<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task is quite easy with Mail.dll IMAP client. IMAP protocol unlike POP3 stores the unseen information on the server. So all we need to do is connect via SSL\/TLS, search for unseen emails, and download them. Mail.dll will perform all the hard work. First thing to remember is to enable IMAP in Gmail (your [&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-262","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\/262"}],"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=262"}],"version-history":[{"count":21,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/262\/revisions"}],"predecessor-version":[{"id":6448,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/262\/revisions\/6448"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}