@cap.aspose,
I have observed your requirements further and like to share that as per my previous code the content inside Body is of HTML type. Therefore, when you get body, you get complete HTML part of text. I suggest you to please try using following sample code to serve the purpose on your end. You may modify this as per your needs accordingly.
public static string HTMLToText(string HTMLCode)
{
// Remove new lines since they are not visible in HTML
HTMLCode = HTMLCode.Replace("\n", " ");
// Remove tab spaces
HTMLCode = HTMLCode.Replace("\t", " ");
// Remove multiple white spaces from HTML
HTMLCode = Regex.Replace(HTMLCode, "\\s+", " ");
// Remove HEAD tag
HTMLCode = Regex.Replace(HTMLCode, "<head.*?</head>", ""
, RegexOptions.IgnoreCase | RegexOptions.Singleline);
// Remove any JavaScript
HTMLCode = Regex.Replace(HTMLCode, "<script.*?</script>", ""
, RegexOptions.IgnoreCase | RegexOptions.Singleline);
// Replace special characters like &, <, >, " etc.
StringBuilder sbHTML = new StringBuilder(HTMLCode);
// Note: There are many more special characters, these are just
// most common. You can add new characters in this arrays if needed
string[] OldWords = {" ", "&", """, "<",
“>”, “®”, “©”, “•”, “™”};
string[] NewWords = { " “, “&”, “””, “<”, “>”, “®”, “©”, “•”, “â„¢” };
for (int i = 0; i < OldWords.Length; i++)
{
sbHTML.Replace(OldWords[i], NewWords[i]);
}
// Check if there are line breaks (<br>) or paragraph (<p>)
sbHTML.Replace("<br>", "\n<br>");
sbHTML.Replace("<br ", "\n<br ");
sbHTML.Replace("<p ", "\n<p ");
// Finally, remove all HTML tags and return plain text
return System.Text.RegularExpressions.Regex.Replace(
sbHTML.ToString(), "<[^>]*>", "");
}
public static void ReadMessageBody()
{
String path = @"C:\Email\Message1\";
MapiMessage msg = MapiMessage.FromFile(path+ "Message1.msg");
if(msg.BodyType==BodyContentType.Html)
{
var body3 = msg.BodyHtml;
String body4 = HTMLToText(body3);
StreamWriter sw = new StreamWriter(path + "output_out.html", false, Encoding.UTF8);
StreamWriter sw2 = new StreamWriter(path + "output_out.txt", false, Encoding.ASCII);
//Writing Paragraphs data to HTML by providing paragraph starting index, total paragraphs to be copied
sw.Write(body3);
sw.Close();
sw2.Write(body4);
sw2.Close();
int ss = 0;
}
else if(msg.BodyType == BodyContentType.PlainText)
{
var body2 = msg.Body;
}
else if (msg.BodyType == BodyContentType.Rtf)
{
var body = msg.BodyRtf;
}
}