diff --git a/util/RfMail.cs b/util/RfMail.cs index a08e258..2437be3 100644 --- a/util/RfMail.cs +++ b/util/RfMail.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Text; using System.Linq; using System.IO; +using System.Text.RegularExpressions; using MailKit.Net.Smtp; using MailKit.Net.Imap; @@ -326,6 +327,35 @@ namespace rockfishCore.Util var name = sender != null ? (!string.IsNullOrEmpty(sender.Name) ? sender.Name : sender.Address) : "someone"; quoted.WriteLine("On {0}, {1} wrote:", message.Date.ToString("f"), name); + + //TODO: get text message just like in reader here for html only message + //sometimes body text is empty. + /* + var theBody = m.GetTextBody(MimeKit.Text.TextFormat.Plain); + if (theBody == null) + { + //might be an html email, try to get the text anyway + var ht = m.HtmlBody; + if (!string.IsNullOrWhiteSpace(ht)) + { + theBody = "**** HTML-ONLY MESSAGE ****"; + theBody += "\r\n=-=-=-=- TEXT CONVERSION =-=-=-=-\r\n"; + theBody += StripHTML(ht, true); + theBody += "\r\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"; + theBody += "\r\n=-=-=-=- SOURCE HTML =-=-=-=-\r\n"; + theBody += ht; + theBody += "\r\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"; + theBody += "\r\n******* END MESSAGE *******"; + } + else + { + theBody = "NO MESSAGE BODY WAS FOUND NEITHER HTML NOR TEXT"; + } + } + sb.AppendLine(theBody); + */ + + using (var reader = new StringReader(message.TextBody)) { string line; @@ -668,7 +698,32 @@ namespace rockfishCore.Util sb.AppendLine(m.Subject); sb.AppendLine(); sb.AppendLine(); - sb.AppendLine(m.GetTextBody(MimeKit.Text.TextFormat.Plain)); + // sb.AppendLine(m.GetTextBody(MimeKit.Text.TextFormat.Plain)); + //TODO: if it's an all html email then body will be empty, so attempt to co-erce it into some readable text because the above is not working for that + + var theBody = m.GetTextBody(MimeKit.Text.TextFormat.Plain); + if (theBody == null) + { + //might be an html email, try to get the text anyway + var ht = m.HtmlBody; + if (!string.IsNullOrWhiteSpace(ht)) + { + theBody = "**** HTML-ONLY MESSAGE ****"; + theBody += "\r\n=-=-=-=- TEXT CONVERSION =-=-=-=-\r\n"; + theBody += StripHTML(ht, true); + theBody += "\r\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"; + theBody += "\r\n=-=-=-=- SOURCE HTML =-=-=-=-\r\n"; + theBody += ht; + theBody += "\r\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"; + theBody += "\r\n******* END MESSAGE *******"; + } + else + { + theBody = "NO MESSAGE BODY WAS FOUND NEITHER HTML NOR TEXT"; + } + } + sb.AppendLine(theBody); + rfMessagePreview preview = new rfMessagePreview(); preview.id = uid; preview.preview = sb.ToString(); @@ -685,6 +740,14 @@ namespace rockfishCore.Util } + //convert html to text + public static string StripHTML(string HTMLText, bool decode = true) + { + Regex reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase); + var stripped = reg.Replace(HTMLText, ""); + var ret = decode ? System.Web.HttpUtility.HtmlDecode(stripped) : stripped; + return ret.Trim(); + } /// ///