Turn a saved message into display HTML with remote images blocked by default. message.eml is in the zip.
dotnet add package RecursiveAI.OneMailRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMail; var message = Mail.Parse( File.ReadAllBytes("message.eml")); File.WriteAllText("message.html", message.ToDisplayHtml().Html); Console.WriteLine("Wrote message.html");
Wrote message.htmlUnzip the example, then dotnet run.
The full program
The sample’s HTML body is:
<p>Hello, reader.</p><script>alert('no')</script> <img src="https://example.com/pixel.png" alt="Remote pixel">
It also contains a text/plain attachment named notes.txt whose content is Local attachment.. The full program saves each attachment under a generated, unique name such as attachments/attachment-1.bin and appends a matching download link. It preserves the bytes without trusting the original filename or extension. OneMail’s attachment list shows names and sizes; the application supplies the links.
Show the result in a sandboxed iframe without allow-scripts or allow-same-origin, as the display documentation specifies.
using System.Net; using RecursiveAI.OneMail; var message = Mail.Parse(File.ReadAllBytes("message.eml")); var view = message.ToDisplayHtml(); Directory.CreateDirectory("attachments"); var downloadLinks = new List<string>(); int index = 0; foreach (var attachment in message.Attachments) { string name = $"attachment-{++index}.bin"; string path = Path.Combine("attachments", name); using var input = attachment.Open(); using var output = File.Create(path); input.CopyTo(output); string label = WebUtility.HtmlEncode(attachment.FileName ?? name); downloadLinks.Add($"<p><a href=\"attachments/{name}\">{label}</a></p>"); } File.WriteAllText("message.html", view.Html + string.Join("", downloadLinks)); Console.WriteLine($"Wrote message.html and {index} attachment(s)"); Console.WriteLine($"Remote references: {view.RemoteReferences.Count}"); foreach (var remote in view.RemoteReferences) Console.WriteLine($"{remote.Outcome}: {remote.Url}"); Console.WriteLine($"Script removed: {!view.Html.Contains("<script")}");
dotnet run prints:
Wrote message.html and 1 attachment(s) Remote references: 1 Blocked: https://example.com/pixel.png Script removed: True
ToDisplayHtml does not throw for message content: a body it cannot read is shown as empty; file I/O errors still propagate.
Variations
Sanitize just an HTML body. This uses the same core sanitization layer without a message header or attachment list. The sanitizer wraps the output in a scoped container (omx-box) to isolate the message layout.
using RecursiveAI.OneMail.Display; var result = HtmlSanitizer.Sanitize( "<p>Hello</p><script>alert(1)</script>"); Console.WriteLine(result.Html);
<style>.omx-box{display:block;position:relative;overflow:hidden;contain:paint;isolation:isolate}</style><div class="omx-box"><div class="omx"><p>Hello</p></div></div>Related recipes
The short answer
Read the saved email, call ToDisplayHtml(), and save the returned Html fragment. The core OneMail package builds a message view with header fields, a sanitized body and an attachment list. By default it blocks remote content and inlines resolved images as data URIs. You do not need a PDF/PNG rendering package to produce HTML.
The result is display HTML, not an exact copy of the original message body. The display documentation describes the rewriting, remote-content policy and framing assumptions. Use the returned report to explain omitted content in your viewer rather than silently treating the output as the original message.
How to convert an email message to HTML?
For EML, the complete example reads the file bytes with Mail.Parse. The shared IMailItem view (a common interface for reading a message’s fields) also supplies ToDisplayHtml for items read from other supported mail formats. The method returns a DisplayResult object containing the HTML and a report of how the body was handled, not just a string. Alongside Html, it includes resolved attachments, unresolved references, remote references and whether the text body was used.
An HTML body passes through HtmlSanitizer. A plain-text body is escaped for HTML display instead. Header values are escaped too. This lets you build a message viewer without putting raw email HTML directly into the host page. It does not turn all email layouts into identical browser output: removed elements, rewritten styles and blocked resources can change what the reader sees.
The example contains a script and an external image. Its output reports the image as blocked and shows that the script element is absent. The program does not launch a browser or request the external URL. Its printed check is a demonstration of this input’s transformation, not a general security test.
How to export email as HTML?
Save DisplayResult.Html using File.WriteAllText, as the snippet at the top does. If you need attachment downloads, the HTML and the attachment bytes are separate outputs. The full program writes each attachment to its own generated local path and appends HTML-escaped download links below the generated fragment. The built-in attachment list contains names and sizes, not download anchors.
For real messages, allocate a unique controlled path for each attachment and use those paths in your application’s download links. Do not use an attachment’s filename directly as a destination: the shared-view documentation exposes names as stored, not as safe filesystem paths. If your viewer serves downloads through an application route, link to that route and enforce access checks there. Escape dynamic link text and attribute values; the example HTML-encodes the displayed filename and generates each link destination itself.
Inline content has a different role from downloadable attachments. The display builder resolves cid: references (links to MIME Content-ID values) and content-location references (links to stored resource URLs) against the message’s attachments; successfully resolved inline resources are removed from the separate attachment list. Missing references remain visible in the result’s unresolved-reference list. A missing image is not an instruction to download an arbitrary URL.
How to embed email into HTML?
Use a separate sandboxed iframe for the fragment rather than mixing untrusted message layout into application chrome. The documented threat model treats the message HTML, CSS, URLs, attachment bytes and names as untrusted, while the caller’s options and callbacks are trusted. Do not give the frame script or same-origin permissions just because the body has passed through a sanitizer.
Remote-content blocking applies to automatic content loading; normal links can still be followed when clicked. If your application must restrict navigation as well, apply its link policy rather than assuming blocked images disable links. The remote-reference report describes policy outcomes, not proof that a URL is benign.
Use HtmlSanitizer.Sanitize directly when you only have body markup. Use ToDisplayHtml when you want the complete message presentation and attachment resolution. Neither output replaces the original EML as an archive: keep the source if you need headers, MIME structure or original bytes later.