This example converts an HTML receipt while keeping its line breaks and list items.
dotnet add package RecursiveAI.OneMailRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMail.Bodies; var html = "<p>Receipt #42<br>Paid</p>" + "<ul><li>Tea & honey</li>" + "<li>Oats</li></ul><p>Total: $12.50</p>"; var text = HtmlToText.Convert(html).Text; Console.WriteLine(text);
Receipt #42 Paid * Tea & honey * Oats Total: $12.50
Unzip the example, then dotnet run.
Use BodySelector.GetText(message) for a parsed email, or HtmlToText.Convert when you already have an HTML string.
The full program
The download builds and parses an email with only a text/html body. BodySelector.GetText returns the converted body in that case, or the stored plain-text body when there is one. The receipt and headers are all in the code; there is no input file.
using RecursiveAI.OneMail; using RecursiveAI.OneMail.Bodies; using RecursiveAI.OneMail.Composition; const string html = """ <p>Receipt #42<br>Paid</p> <ul><li>Tea & honey</li><li>Oats</li></ul> <p>Total: $12.50</p> """; var outgoing = new OutgoingMessage { From = "store@example.com", To = ["buyer@example.org"], Subject = "Receipt #42", Html = html }; var message = Mail.Parse(outgoing.ToEntity().ToArray()); var body = BodySelector.GetText(message) ?? throw new InvalidOperationException("No text or HTML body."); Console.WriteLine(body.ReadText());
Receipt #42 Paid * Tea & honey * Oats Total: $12.50
If the message has neither text nor HTML to convert, BodySelector.GetText returns null.
Variations
If you want a link’s visible label without its destination, use LinkStyle.TextOnly:
using RecursiveAI.OneMail.Bodies; var html = """ <a href="https://example.org/42">Receipt</a> """; var options = new HtmlToTextOptions { Links = LinkStyle.TextOnly }; Console.WriteLine(HtmlToText.Convert(html, options).Text);
ReceiptRelated recipes
Parse an EML file · Render email as HTML
The short answer
For a decoded HTML string, use HtmlToText.Convert(html).Text. For a parsed email, use BodySelector.GetText(message) so you get the author’s plain-text version when there is one. The HTML conversion is a fallback, not a rewrite of the original email.
How do I convert HTML email to plain text without losing formatting?
Keep the structure that plain text can express. HtmlToText turns a <br> into a line break, separates paragraphs with a blank line, gives unordered lists * markers, and numbers ordered lists. It decodes character references, so Tea & honey becomes Tea & honey. Simply removing tags could leave the item names running together. The conversion does not reproduce visual styling such as colors or font sizes.
This is body conversion, not HTML sanitization. The converter reads the email’s HTML and returns a string for plain-text use; it does not produce replacement markup. If you put that string back into a web page, display it as text, not as HTML.
How do I display HTML email as plain text?
Use BodySelector.GetText for a parsed message rather than converting its HTML unconditionally. Some messages carry both a text/plain part and a text/html part. Using the stored plain-text part preserves the author’s wording and line breaks. The download exercises the other case: when the author provides only HTML, OneMail derives the fallback from it. ReadText() reads the selected result as a string.
With a decoded HTML string as input, HtmlToText.Convert returns conversion issues alongside the text. Check Issues if a preview looks incomplete; for example, an unusually long body can reach the character limit and return partial text. See the body conversion reference for the relevant options and issue names.