RG.RazorMail 1.9.1

dotnet add package RG.RazorMail --version 1.9.1                
NuGet\Install-Package RG.RazorMail -Version 1.9.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RG.RazorMail" Version="1.9.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RG.RazorMail --version 1.9.1                
#r "nuget: RG.RazorMail, 1.9.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RG.RazorMail as a Cake Addin
#addin nuget:?package=RG.RazorMail&version=1.9.1

// Install RG.RazorMail as a Cake Tool
#tool nuget:?package=RG.RazorMail&version=1.9.1                

RG.RazorMail

NuGet .NET

Render razor view to html and inline css. This library simply wraps RazorLight and PreMailer.Net

Setup

1. For Rendering .cshtml Views

1.1. Add to service collection
services.AddRazorMail(options => {
    options.ViewAssembly = Assembly.GetEntryAssembly();
});
1.2. Set PreserveCompilationContext to true in your .csproj file
<PropertyGroup>
  <PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
1.3. Set build action of your razor views to EmbeddedResource
1.4. Use relative path of your razor views as viewName
string html = await _razorMail.RenderAsync("Views/Home/Index.cshtml", model);

2. For Rendering .razor Components

2.1. Add to service collection
services.AddRazorMail();

Available methods

// Render razor view to string
Task<string> RenderAsync<T>(string viewName, T model);

// Render razor component to string
Task<string> RenderComponentAsync<TComponent>(IDictionary<string, object>? parameters);

// Render razor view to string and inline css
Task<string> RenderAndInlineCssAsync<T>(string viewName, T model, string? css = null);

// Render razor component to string and inline css
Task<string> RenderComponentAndInlineCssAsync<TComponent>(IDictionary<string, object>? parameters, string? css = null);

// Inline css only
string InlineCss(string html, string? css = null);

Benchmark Results

Intel Core i7-10700 CPU 2.90GHz, 1 CPU, 16 logical and 8 physical cores .NET SDK 8.0.101

Method Mean Error StdDev Rank Gen0 Gen1 Allocated
'Render .cshtml View' 67.514 us 0.8551 us 0.5088 us 3 0.8545 - 7.06 KB
'Render .razor Component' 2.808 us 0.1450 us 0.0759 us 1 0.1984 0.0648 1.64 KB
'Render+Inline .cshtml View' 184.443 us 83.4985 us 55.2291 us 4 6.8359 0.4883 59.22 KB
'Render+Inline .razor Component' 43.990 us 1.3244 us 0.6927 us 2 5.8594 1.7090 48.73 KB

Intel Core i7-10700 CPU 2.90GHz, 1 CPU, 16 logical and 8 physical cores .NET SDK 9.0.100

Method Mean Error StdDev Rank Gen0 Gen1 Allocated
'Render .cshtml View' 21.279 us 0.0556 us 0.0331 us 2 0.7935 - 6.95 KB
'Render .razor Component' 4.108 us 1.4003 us 0.9262 us 1 0.1907 0.0610 1.59 KB
'Render+Inline .cshtml View' 76.252 us 8.5756 us 4.4852 us 4 7.3242 0.4883 59.86 KB
'Render+Inline .razor Component' 37.275 us 5.6494 us 3.3619 us 3 6.1035 1.4648 51.13 KB
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.9.1 87 12/5/2024
1.9.0 105 11/13/2024
1.0.7 2,575 2/15/2024
1.0.6 115 2/15/2024
1.0.5 134 2/15/2024
1.0.4 122 2/14/2024
1.0.3 132 2/13/2024
1.0.2 423 11/13/2023
1.0.1 848 11/30/2022
1.0.0 597 3/2/2022