PreMailer.Net 2.5.0

PreMailer.Net .NET Core build Nuget count

C# Library for moving CSS to inline style attributes, to gain maximum E-mail client compatibility.

Usage

Static method on PreMailer class

string htmlSource = File.ReadAllText(@"C:\Workspace\testmail.html");

var result = PreMailer.MoveCssInline(htmlSource);

result.Html 		// Resultant HTML, with CSS in-lined.
result.Warnings 	// string[] of any warnings that occurred during processing.

Set up PreMailer instance

string htmlSource = File.ReadAllText(@"C:\Workspace\testmail.html");

var pm = new PreMailer(htmlSource);
pm.AddAnalyticsTags(source, medium, campaign, content, domain = null); // Optional to add analytics tags

var result = pm.MoveCssInline(...);

result.Html 		// Resultant HTML, with CSS in-lined.
result.Warnings 	// string[] of any warnings that occurred during processing.

Options

The following options can be passed to the PreMailer.MoveCssInline method to configure it's behaviour:

  • baseUri(Uri = null) - Base URL to apply to link elements with href values ending with .css.
  • removeStyleElements(bool = false) - Removes elements that were used to source CSS (currently, only style is supported).
  • ignoreElements(string = null) - CSS selector of element(s) not to inline. Useful for mobile styles (see below).
  • css(string = null) - A string containing a style-sheet for inlining.
  • stripIdAndClassAttributes(bool = false) - True to strip ID and class attributes.
  • removeComments(bool = false) - True to remove comments, false to leave them intact.

External style sheets

Sometimes it's handy to reference external style sheets with a <link href="..." /> element. PreMailer will download and use external style sheets as long as the value of href ends with .css.

Both absolute and relative URLs are supported. If the URL is relative, you must specify the baseUri parameter in either the constructor, or when calling the static MoveCssInline method.

<link /> elements that match the ignoreElements selector won't be downloaded.

Media queries

If you want to apply mobile styles to your e-mail, you should put your mobile specific styles in its own style block that targets the appropriate devices using media queries.

But since you cannot know by the time of sending an e-mail whether or not it will be viewed on a mobile device, the style block that targets mobile devices should not be inlined!

To ignore a style block, you need to specify an ignore selector when calling the MoveCssInline method, like this:

var result = PreMailer.MoveCssInline(input, false, ignoreElements: "#ignore");

And your mobile specific style block should have an ID of ignore:

<style type="text/css" id="ignore">.target { width: 1337px; }</style>

Premailer specific CSS becomes HTML attributes

Premailer looks for the use of CSS attributes prefixed with -premailer and will proxy the value through to the DOM element as an attribute.

For example

table {
    -premailer-cellspacing: 5;
    -premailer-width: 500;
}

will make a table element render as

<table cellspacing="5" width="500">

Custom DOM Processing

using(var pm = new PreMailer(html)){

  var document = pm.Document;

  // use AngleSharp to process document before moving css inline ...

  var result = pm.MoveCssInline();
}

Notes

  • Pseudo classes/elements which not supported by external dependencies, or doesn't make sense in email, will be ignored and logged to the InlineResult.Warnings collection.

Installation

NuGet: PreMailer.Net

Contributors

Among others

License

PreMailer.Net is available under the MIT license. See the LICENSE file for more info.

Showing the top 20 packages that depend on PreMailer.Net.

Packages Downloads
WillowMedia.Cms
WillowMedia.Cms
114
WillowMedia.Cms
WillowMedia.Cms
71
WillowMedia.Cms
WillowMedia.Cms
68
WillowMedia.Cms
WillowMedia.Cms
64
WillowMedia.Cms
WillowMedia.Cms
52
WillowMedia.Cms
WillowMedia.Cms
44
WillowMedia.Cms
WillowMedia.Cms
39
WillowMedia.Cms
WillowMedia.Cms
29
WillowMedia.Cms
WillowMedia.Cms
26
WillowMedia.Cms
WillowMedia.Cms
24
WillowMedia.Cms
WillowMedia.Cms
21
WillowMedia.Cms
WillowMedia.Cms
20
WillowMedia.Cms
WillowMedia.Cms
19
WillowMedia.Cms
WillowMedia.Cms
17
WillowMedia.Cms
WillowMedia.Cms
15

What's Changed * Bump AngleSharp from 0.16.1 to 1.0.7. Only the IMarkupFormatter from AngleSharp is exposed as public api and that interface has not changed in that version jump so this should not cause any breakages for users. * Dependency Bumps for the Tests and Benchmark projects. * Fix race condition when running unit tests by @jasekiw in https://github.com/milkshakesoftware/PreMailer.Net/pull/374 * Normalize all file line endings by @jasekiw in https://github.com/milkshakesoftware/PreMailer.Net/pull/365 * Update benchmark project framework by @jasekiw in https://github.com/milkshakesoftware/PreMailer.Net/pull/351 * Use correct framework by @kasperk81 https://github.com/milkshakesoftware/PreMailer.Net/pull/334 * Update CI triggers to main branch by @martinnormark https://github.com/milkshakesoftware/PreMailer.Net/pull/332 * Bug triage by @martinnormark https://github.com/milkshakesoftware/PreMailer.Net/pull/328 * More efficient selector matching of all elements by @patrikwlund https://github.com/milkshakesoftware/PreMailer.Net/pull/326 * Read text node instead of InnerHtml for CSS by @patrikwlund https://github.com/milkshakesoftware/PreMailer.Net/pull/325 * Include a benchmark that sets all MoveCssInline() flags by @patrikwlund https://github.com/milkshakesoftware/PreMailer.Net/pull/324 * Realistic benchmark by @patrikwlund https://github.com/milkshakesoftware/PreMailer.Net/pull/323 * Decent optimizations for CssAttributeCollection by @patrikwlund https://github.com/milkshakesoftware/PreMailer.Net/pull/322 New Contributors * @patrikwlund made their first contribution in https://github.com/milkshakesoftware/PreMailer.Net/pull/323 * @kasperk81 made their first contribution in https://github.com/milkshakesoftware/PreMailer.Net/pull/334 * @jasekiw made their first contribution in https://github.com/milkshakesoftware/PreMailer.Net/pull/351 Full Changelog: https://github.com/milkshakesoftware/PreMailer.Net/compare/v2.4.0...v2.5.0

.NET Framework 4.6.1

.NET Standard 2.0

Version Downloads Last updated
2.7.2 281 06/27/2025
2.7.1 16 06/27/2025
2.7.0 24 06/11/2025
2.6.0 17 05/29/2025
2.5.0 4 06/28/2025
2.4.0 4 06/28/2025
2.3.0 4 06/28/2025
2.2.0 5 06/28/2025
2.1.3 4 06/28/2025
2.1.1 4 06/28/2025
2.1.0 4 06/28/2025
2.0.1 4 06/28/2025
2.0.0 4 06/28/2025
2.0.0-beta2 3 06/27/2025
2.0.0-beta1 4 06/27/2025
1.5.5 4 06/28/2025
1.5.4 4 06/28/2025
1.5.3 4 06/28/2025
1.5.2 4 06/28/2025
1.5.1 5 06/28/2025
1.5.0 4 06/28/2025
1.4.3 4 06/28/2025
1.4.2 4 06/28/2025
1.4.1 4 06/28/2025
1.4.0 5 06/28/2025
1.3.1 5 06/28/2025
1.3.0 4 06/28/2025
1.2.9 4 06/28/2025
1.2.8 5 06/28/2025
1.2.7 4 06/28/2025
1.2.6 4 06/28/2025
1.2.5 4 06/28/2025
1.2.4 4 06/28/2025
1.2.3 4 06/28/2025
1.2.2 4 06/28/2025
1.2.1 4 06/28/2025
1.2.0 4 06/28/2025
1.1.3 5 06/28/2025
1.1.2 4 06/28/2025
1.1.1 4 06/28/2025
1.1.0 4 06/28/2025
1.0.0 6 06/17/2025