{"id":1390,"date":"2010-10-25T19:26:04","date_gmt":"2010-10-25T17:26:04","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1390"},"modified":"2014-04-15T15:39:32","modified_gmt":"2014-04-15T13:39:32","slug":"access-custom-email-header-using-imap","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/access-custom-email-header-using-imap","title":{"rendered":"Access custom email header using IMAP"},"content":{"rendered":"<p>Most common email headers like <a href=\"\/blog\/get-common-email-fields-subject-text-with-imap\">From, Subject, Date<\/a>, <a href=\"\/blog\/how-to-access-to-cc-bcc-fields\">To, Cc<\/a> etc. are exposed by <em>IMail <\/em>interface as properties. Those properties contain parsed information and return <strong>strong typed objects<\/strong> like <em>DateTime<\/em>&#8216;s, string collections, <em>MailAddress <\/em>collections.<\/p>\n<p>There are times however that you need to access non-standard email header. Good example is a need to get a spam value that your IMAP server assigns to every email by inserting additional &#8216;x-spam&#8217; header.<\/p>\n<p>Mail.dll MIME and S\/MIME parser allows users to access entire MIME tree of the message. <em>IMail.Document<\/em> property returns MIME document that represent this email message. You can use <em>IMail.Document.Root.Headers<\/em> collection to <strong>access any non-standard and standard header<\/strong>.<\/p>\n<p>If you are using IMAP client you can also <a href=\"\/blog\/how-to-search-imap-in-net\">search IMAP server<\/a> for messages that have specified header or have specified value in header. <\/p>\n<p>If you only need to access email headers, consider using <em>GetHeadersByUID<\/em> method, which doesn&#8217;t download entire message from IMAP server just headers. The sample below on the other hand downloads entire message.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\nusing System;\r\nusing Limilabs.Mail;\r\nusing Limilabs.Client.IMAP;\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;);    \/\/ or ConnectSSL for SSL\r\n            imap.Login(&quot;user&quot;, &quot;password&quot;);\r\n\r\n            MailBuilder builder = new MailBuilder();\r\n            foreach (long uid in imap.GetAll())\r\n            {\r\n                var eml = imap.GetMessageByUID(uid);\r\n                IMail email = builder.CreateFromEml(eml);\r\n                Console.WriteLine(&quot;subject: {0}&quot;, email.Subject);\r\n\r\n                \/\/ Get custom header:\r\n                string header = email.Document.Root.Headers&#x5B;&quot;x-spam&quot;];\r\n                Console.WriteLine(&quot;x-spam: {0}&quot;, header);\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 version\r\n\r\nImports System\r\nImports Limilabs.Mail\r\nImports Limilabs.Client.IMAP\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;imap.example.com&quot;)    ' or ConnectSSL for SSL\r\n            imap.Login(&quot;user&quot;, &quot;password&quot;)\r\n\r\n            Dim builder As New MailBuilder()\r\n            For Each uid As String In imap.GetAll()\r\n                Dim eml = imap.GetMessageByUID(uid)\r\n                Dim email As IMail = builder.CreateFromEml(eml)\r\n                Console.WriteLine(&quot;subject: {0}&quot;, email.Subject)\r\n\r\n                ' Get custom header:\r\n                Dim header As String = email.Document.Root.Headers(&quot;x-spam&quot;)\r\n                Console.WriteLine(&quot;x-spam: {0}&quot;, header)\r\n            Next\r\n            imap.Close()\r\n        End Using\r\n\r\n    End Sub\r\nEnd Module\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Most common email headers like From, Subject, Date, To, Cc etc. are exposed by IMail interface as properties. Those properties contain parsed information and return strong typed objects like DateTime&#8216;s, string collections, MailAddress collections. There are times however that you need to access non-standard email header. Good example is a need to get a spam [&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,28,77,57],"class_list":["post-1390","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-imap","tag-imap-component","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1390"}],"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=1390"}],"version-history":[{"count":8,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1390\/revisions"}],"predecessor-version":[{"id":4563,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1390\/revisions\/4563"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}