{"id":2185,"date":"2011-12-27T18:59:24","date_gmt":"2011-12-27T16:59:24","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=2185"},"modified":"2012-06-03T15:12:50","modified_gmt":"2012-06-03T13:12:50","slug":"extract-plain-text-from-html-email","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/extract-plain-text-from-html-email","title":{"rendered":"Extract plain text from HTML email"},"content":{"rendered":"<p>Mail.dll <a href=\"\/mail\">MIME and email component<\/a> may be used to get the plain-text body and HTML body from any email message.<\/p>\n<p>If a message contains plain-text, no conversion is necessary. It\u2019s simply a matter of using the <em>Text <\/em>property of <em>IMail <\/em>interface.<\/p>\n<p>If however the email does not contain plain-text and only HTML content is available, <strong>GetTextFromHtml <\/strong>method may be used to <strong>convert the HTML to plain-text<\/strong>.<\/p>\n<p>The internal conversion process is much more sophisticated than what can be accomplished with the simple regular-expression code. Converting HTML to plain text is much more than simply removing HTML tags from an HTML document.<\/p>\n<p>Mail.dll contains <strong>full-blown HTML parser<\/strong> that handles script tags, comments, CDATA and even incorrectly formatted HTML.<\/p>\n<p>The following C# and VB.NET code extracts plain-text from the HTML body of the email message:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nIMail email = ...\r\n\r\nstring text = &quot;&quot;; \r\nif (email.IsText)\r\n    text = email.Text;\r\nelse if (email.IsHtml)\r\n    text = email.GetTextFromHtml();\r\nConsole.WriteLine(text);\r\n\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim email As IMail = ...\r\n\r\nDim text As String = &quot;&quot;\r\nIf email.IsText Then\r\n    text = email.Text\r\nElseIf email.IsHtml Then\r\n    text = email.GetTextFromHtml()\r\nEnd If\r\nConsole.WriteLine(text)\r\n<\/pre>\n<p>You can also use <strong>GetBodyAsText <\/strong>method that<strong> returns body in plain text format<\/strong> (it uses <em>IMail.Text<\/em> property or <em>GetTextFromHtml<\/em> method).<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nIMail email = ...\r\n\r\nstring text = email.GetBodyAsText();\r\nConsole.WriteLine(text);\r\n\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim email As IMail = ...\r\n\r\nDim text As String = email.GetBodyAsText()\r\nConsole.WriteLine(text)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Mail.dll MIME and email component may be used to get the plain-text body and HTML body from any email message. If a message contains plain-text, no conversion is necessary. It\u2019s simply a matter of using the Text property of IMail interface. If however the email does not contain plain-text and only HTML content is available, [&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,72,57],"class_list":["post-2185","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-email-component","tag-html","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/2185"}],"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=2185"}],"version-history":[{"count":3,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/2185\/revisions"}],"predecessor-version":[{"id":2627,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/2185\/revisions\/2627"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=2185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=2185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=2185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}