PreMailer.Net 2.7.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);
// Optional to add analytics tags
pm.AddAnalyticsTags(source, medium, campaign, content, domain = null);

var result = pm.MoveCssInline(
    removeStyleElements: false,
    ignoreElements: "#ignore",
    preserveMediaQueries: true
);

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

Options

The following options can be passed to the PreMailer.MoveCssInline method to configure its behavior:

  • 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.
  • customFormatter(IMarkupFormatter = null) - Custom formatter to use for the HTML output.
  • preserveMediaQueries(bool = false) - If true and removeStyleElements is true, it will preserve media queries in the style node while removing other CSS, instead of removing the entire style node.
  • useEmailFormatter(bool = false) - If true, empty HTML tags will be preserved as full tags instead of being converted to self-closing tags, and HTML entities like © will be preserved.

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");

Alternatively, you can use the preserveMediaQueries parameter to keep your media queries while removing other styles:

var result = PreMailer.MoveCssInline(input, removeStyleElements: true, preserveMediaQueries: true);

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">

Analytics Tags

The AddAnalyticsTags method can be used to add Google Analytics tracking parameters to links in your HTML:

var pm = new PreMailer(htmlSource);
pm.AddAnalyticsTags(
    source: "newsletter",       // utm_source parameter
    medium: "email",            // utm_medium parameter
    campaign: "summer_sale",    // utm_campaign parameter
    content: "logo_link",       // utm_content parameter
    domain: "example.com"       // Optional: only add tags to links matching this domain
);

This will append ?utm_source=newsletter&utm_medium=email&utm_campaign=summer_sale&utm_content=logo_link to all links, or to links matching the specified domain if provided.

Custom DOM Processing

The Document property provides access to the underlying IHtmlDocument object, allowing you to perform custom DOM manipulation before inlining CSS:

using(var pm = new PreMailer(html)){
  var document = pm.Document;

  // Use AngleSharp to process document before moving css inline...
  
  var result = pm.MoveCssInline();
}

This is useful for advanced scenarios where you need to modify the HTML structure before applying CSS.

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
evernote-cloud-sdk-windows
Evernote Cloud SDK for Windows
19
evernote-cloud-sdk-windows
Evernote Cloud SDK for Windows
18
evernote-cloud-sdk-windows
Evernote Cloud SDK for Windows
16
evernote-cloud-sdk-windows
Evernote Cloud SDK for Windows
15
evernote-cloud-sdk-windows
Evernote Cloud SDK for Windows
13

What's Changed * Build(deps): Bump actions/checkout from 4.1.6 to 4.1.7 by @dependabot in https://github.com/milkshakesoftware/PreMailer.Net/pull/409 * Build(deps): Bump actions/setup-dotnet from 4.0.0 to 4.0.1 by @dependabot in https://github.com/milkshakesoftware/PreMailer.Net/pull/411 * Build(deps): Bump pascalgn/automerge-action from 0.16.3 to 0.16.4 by @dependabot in https://github.com/milkshakesoftware/PreMailer.Net/pull/412 * Build(deps): Bump actions/checkout from 4.1.7 to 4.2.0 by @dependabot in https://github.com/milkshakesoftware/PreMailer.Net/pull/413 * Build(deps): Bump actions/checkout from 4.2.0 to 4.2.1 by @dependabot in https://github.com/milkshakesoftware/PreMailer.Net/pull/414 * Build(deps): Bump actions/checkout from 4.2.1 to 4.2.2 by @dependabot in https://github.com/milkshakesoftware/PreMailer.Net/pull/415 * Build(deps): Bump actions/setup-dotnet from 4.0.1 to 4.1.0 by @dependabot in https://github.com/milkshakesoftware/PreMailer.Net/pull/416 * Build(deps): Bump actions/setup-dotnet from 4.1.0 to 4.2.0 by @dependabot in https://github.com/milkshakesoftware/PreMailer.Net/pull/418 * Update target frameworks of test and benchmark projects by @martinnormark in https://github.com/milkshakesoftware/PreMailer.Net/pull/420 * LinkTagCssSource now also downloads content from @import declarations. by @whorchner in https://github.com/milkshakesoftware/PreMailer.Net/pull/419 * Build(deps): Bump actions/setup-dotnet from 4.2.0 to 4.3.0 by @dependabot in https://github.com/milkshakesoftware/PreMailer.Net/pull/421 * Build(deps): Bump actions/setup-dotnet from 4.3.0 to 4.3.1 by @dependabot in https://github.com/milkshakesoftware/PreMailer.Net/pull/423 * Fix #410: Preserve !important tag in inlined styles by @devin-ai-integration in https://github.com/milkshakesoftware/PreMailer.Net/pull/424 * Fix #347: Preserve HTML entities like &copy; during processing by @devin-ai-integration in https://github.com/milkshakesoftware/PreMailer.Net/pull/425 * Update README to accurately document PreMailer API by @devin-ai-integration in https://github.com/milkshakesoftware/PreMailer.Net/pull/426 * Fix #171: Preserve empty HTML tags during serialization by @devin-ai-integration in https://github.com/milkshakesoftware/PreMailer.Net/pull/427 * Fix CSS comment regex to handle protocol-agnostic URLs in premailer attributes by @devin-ai-integration in https://github.com/milkshakesoftware/PreMailer.Net/pull/428 * Fix issue #235: Preserve base64 encoded images and URLs during CSS parsing and style application by @devin-ai-integration in https://github.com/milkshakesoftware/PreMailer.Net/pull/429 Full Changelog: https://github.com/milkshakesoftware/PreMailer.Net/compare/v2.6.0...v2.7.0

.NET Framework 4.6.1

.NET Standard 2.0

Version Downloads Last updated
2.7.0 1 06/07/2025
2.6.0 32 06/08/2024
2.5.0 16 12/05/2023
2.4.0 16 06/13/2023
2.3.0 13 06/13/2023
2.2.0 16 06/13/2023
2.1.3 15 06/13/2023
2.1.1 12 06/12/2023
2.1.0 15 06/12/2023
2.0.1 11 06/12/2023
2.0.0 16 06/13/2023
2.0.0-beta2 8 10/20/2023
2.0.0-beta1 17 09/24/2023
1.5.5 14 06/13/2023
1.5.4 12 06/13/2023
1.5.3 11 06/13/2023
1.5.2 12 06/13/2023
1.5.1 14 06/12/2023
1.5.0 13 06/13/2023
1.4.3 13 06/13/2023
1.4.2 14 06/13/2023
1.4.1 11 06/13/2023
1.4.0 16 06/12/2023
1.3.1 12 06/13/2023
1.3.0 15 06/13/2023
1.2.9 12 06/13/2023
1.2.8 12 06/12/2023
1.2.7 14 06/13/2023
1.2.6 11 06/13/2023
1.2.5 12 06/13/2023
1.2.4 12 06/12/2023
1.2.3 15 06/13/2023
1.2.2 13 06/12/2023
1.2.1 10 06/12/2023
1.2.0 13 06/13/2023
1.1.3 13 02/17/2024
1.1.2 11 06/12/2023
1.1.1 15 06/12/2023
1.1.0 14 06/13/2023
1.0.0 14 06/13/2023