C# Library for moving CSS to inline style attributes, to gain maximum E-mail client compatibility.
PreMailer
classstring 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.
PreMailer
instancestring 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.
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.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.
If you want to apply mobile styles to your e-mail, you should put yourmobile 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 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">
using(var pm = new PreMailer(html)){
var document = pm.Document;
// use AngleSharp to process document before moving css inline ...
var result = pm.MoveCssInline();
}
InlineResult.Warnings
collection.NuGet: PreMailer.Net
PreMailer.Net is available under the MIT license. See the LICENSE file for more info.
.NET Core的邮件组件你用过哪些呢?MailKit吗?其实还有。下面是收集github中开源的邮件组件,感兴趣的童鞋可以点击链接去看看详细使用说明。 1、FluentEmail:电子邮件发送库。 FluentEmail是一款在GitHub上开源免费的支持.Net和.Net Core邮件发送组件,有两个接口“FluentEmail.Core 和FluentEmail.Smtp ”,第一
报表邮件推送方案: 1.Sqlserver报表=》直接将数据库的数据进行整理后进行发出,样式较为固定且高亮显示重要数据时,相对比较麻烦。优点是显示图表相对比较方便; 2.C#直接编写后台代码=》从数据库获取数据后,利用stringbuilder将html代码拼接起来,可自定义显示样式。但显示统计图时,只能迂回方案来实现。 最近在搞项目的时候,发现采用下述邮件推送的方式,可以既能生成邮件图表数据,又
API 框架 NancyFx:轻量、用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台。官网 ASP.NET WebAPI:快捷创建 HTTP 服务的框架,可以广泛用于多种不同的客户端,包括浏览器和移动设备。官网 ServiceStack:架构缜密、速度飞快、令人愉悦的 web 服务。官网 Nelibur:Nelibur 是一个使用纯 WCF 构