{"id":1221,"date":"2010-10-25T14:56:35","date_gmt":"2010-10-25T12:56:35","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1221"},"modified":"2014-04-15T17:31:04","modified_gmt":"2014-04-15T15:31:04","slug":"get-common-email-fields-subject-text-with-imap","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/get-common-email-fields-subject-text-with-imap","title":{"rendered":"Get common email fields (Subject, Text) with IMAP"},"content":{"rendered":"<p>In this article we&#8217;ll show how to download emails using Mail.dll <a href=\"\/mail\">IMAP client<\/a> and <strong>access some common email properties<\/strong> like: Subject, Date, Html, Text and Attachments.<\/p>\n<p>Remember about adding a reference to Mail.dll and importing namespaces.<\/p>\n<p>First you need to download email from IMAP server using <em>Imap <\/em>class. Then parse it using <em>MailBuilder <\/em>class. After that you&#8217;ll receive <em>IMail <\/em>interface.<\/p>\n<p>This interface can be used to access all standard and <a href=\"\/blog\/access-custom-email-header-using-imap\">non-standard headers<\/a>.<br \/>\nSubject, From, To, Date, email data like: Text and Html versions of the email, and others can be accessed as simple properties. This sample also shows how to access Attachments collection.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing Limilabs.Client.IMAP;\r\nusing Limilabs.Mail;\r\nusing Limilabs.Mail.MIME;\r\nusing Limilabs.Mail.Headers;\r\n\r\nclass Program\r\n{\r\n    static void Main(string&#x5B;] args)\r\n    {\r\n        using(Imap imap = new Imap())\r\n        {\r\n            imap.Connect(&quot;imap.example.com&quot;);    \/\/ use ConnectSSL for SSL\r\n            imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n\r\n            imap.SelectInbox();\r\n            List&lt;long&gt; uids = imap.Search(Flag.Unseen);\r\n\r\n            foreach (long uid in uids)\r\n            {\r\n                var eml = imap.GetMessageByUID(uid);\r\n                IMail email = new MailBuilder()\r\n                    .CreateFromEml(eml);\r\n\r\n                \/\/ Subject\r\n                Console.WriteLine(email.Subject);\r\n\r\n                \/\/ From\r\n                foreach (MailBox m in email.From)\r\n                {\r\n                    Console.WriteLine(m.Address);\r\n                    Console.WriteLine(m.Name);\r\n                }\r\n\r\n                \/\/ Date\r\n                Console.WriteLine(email.Date);\r\n\r\n                \/\/ Text body of the message\r\n                Console.WriteLine(email.Text);\r\n\r\n                \/\/ Html body of the message\r\n                Console.WriteLine(email.Html);\r\n\r\n                \/\/ Custom header\r\n                Console.WriteLine(email.Document.Root.Headers&#x5B;&quot;x-spam&quot;]);\r\n\r\n                \/\/ Save all attachments to disk\r\n                foreach (MimeData mime in email.Attachments)\r\n                {\r\n                    mime.Save(@&quot;c:\\&quot; + mime.SafeFileName);\r\n                }\r\n            }\r\n            imap.Close();\r\n        }\r\n    }\r\n};\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nImports Limilabs.Client.IMAP\r\nImports Limilabs.Mail\r\nImports Limilabs.Mail.MIME\r\nImports Limilabs.Mail.Headers\r\nImports System\r\nImports System.Collections.Generic\r\n\r\nPublic Module Module1\r\n    Public Sub Main(ByVal args As String())\r\n\r\n        Using imap As New Imap()\r\n            imap.Connect(&quot;pop3.example.com&quot;)    ' use ConnectSSL for SSL\r\n            imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\r\n            imap.SelectInbox()\r\n            Dim uids As List(Of Long) = imap.Search(Flag.Unseen)\r\n\r\n            For Each uid As Long In uids\r\n                Dim eml = imap.GetMessageByUID(uid)\r\n                Dim email As IMail = New MailBuilder() _\r\n                    .CreateFromEml(eml)\r\n\r\n                ' Subject\r\n                Console.WriteLine(email.Subject)\r\n\r\n                ' From\r\n                For Each m As MailBox In email.From\r\n                    Console.WriteLine(m.Address)\r\n                    Console.WriteLine(m.Name)\r\n                Next\r\n\r\n                ' Date\r\n                Console.WriteLine(email.&#x5B;Date])\r\n\r\n                ' Text body of the message\r\n                Console.WriteLine(email.Text)\r\n\r\n                ' Html body of the message\r\n                Console.WriteLine(email.Html)\r\n\r\n                ' Custom header\r\n                Console.WriteLine(email.Document.Root.Headers(&quot;x-spam-value&quot;))\r\n\r\n                ' Save all attachments to disk\r\n                For Each mime As MimeData In email.Attachments\r\n                    mime.Save(&quot;c:\\&quot; + mime.SafeFileName)\r\n                Next\r\n            Next\r\n            imap.Close()\r\n        End Using\r\n\r\n    End Sub\r\nEnd Module\r\n<\/pre>\n<p>To, Cc and Bcc headers are a bit tricky as they can contain address groups. You can learn more about how to work with them in <a href=\"\/blog\/how-to-access-to-cc-bcc-fields\">how to access To, Cc, Bcc fields<\/a> article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;ll show how to download emails using Mail.dll IMAP client and access some common email properties like: Subject, Date, Html, Text and Attachments. Remember about adding a reference to Mail.dll and importing namespaces. First you need to download email from IMAP server using Imap class. Then parse it using MailBuilder class. After [&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,28,77,57],"class_list":["post-1221","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-email-component","tag-imap","tag-imap-component","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1221"}],"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=1221"}],"version-history":[{"count":10,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1221\/revisions"}],"predecessor-version":[{"id":2702,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1221\/revisions\/2702"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}