TRENZ.Lib.RazorMail.SystemNet 2.2.0

dotnet add package TRENZ.Lib.RazorMail.SystemNet --version 2.2.0                
NuGet\Install-Package TRENZ.Lib.RazorMail.SystemNet -Version 2.2.0                
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="TRENZ.Lib.RazorMail.SystemNet" Version="2.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TRENZ.Lib.RazorMail.SystemNet --version 2.2.0                
#r "nuget: TRENZ.Lib.RazorMail.SystemNet, 2.2.0"                
#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 TRENZ.Lib.RazorMail.SystemNet as a Cake Addin
#addin nuget:?package=TRENZ.Lib.RazorMail.SystemNet&version=2.2.0

// Install TRENZ.Lib.RazorMail.SystemNet as a Cake Tool
#tool nuget:?package=TRENZ.Lib.RazorMail.SystemNet&version=2.2.0                

MIT License Core NuGet Version MailKit NuGet Version SystemNet NuGet Version NuGet Downloads

TRENZ.Lib.RazorMail

Templated transactional e-mail using Razor

This is a simple library you can use to write e-mail templates in Razor syntax. That means you write raw HTML, but elevated with C# β€” you get @foreach, @switch, and so on, and you get a strongly-typed model for custom data.

Installation

In NuGet, reference either the TRENZ.Lib.RazorMail.SystemNet or the TRENZ.Lib.RazorMail.MailKit package, depending on which MailSender backend you prefer. MailKit is more modern and powerful, but System.Net.Mail comes built into .NET. There is no need to reference TRENZ.Lib.RazorMail directly.

Via dotnet:

dotnet add package TRENZ.Lib.RazorMail.SystemNet

…or:

dotnet add package TRENZ.Lib.RazorMail.MailKit

Usage

A simple template looks like so:

@using TRENZ.Lib.RazorMail.SampleWebApi.Models

@inherits TRENZ.Lib.RazorMail.Core.MailTemplateBase<SampleModel>

@{
    Subject = "Greetings!";
}

<!DOCTYPE html>

<html lang="en">
<head>
    <title></title>
</head>
<body>
    <h1>@Model.Salutation!</h1>
</body>
</html>

Where SampleModel is:

public record SampleModel(string Salutation);

Notice that:

  • we're passing SampleModel as our model type. It has a property Salutation, so we can then do @Model.Salutation to get its value.
  • we can set the Subject property, which becomes the e-mail subject.

Attachments

Inheriting from TRENZ.Lib.RazorMail.Core.MailTemplateBase<T> also gives us the convenience methods AttachFile() and InlineFile(). These differ only in whether an attachment is intended for download, or for inline display.

For example, to show an image inline, you simply do:

<img src="@InlineFile("My Company Logo.png")" />

That's it. This attaches the image as a file, then references it using cid format[^1]. Because the image is attached, this also doesn't require your users to enable loading external images, which some mail clients restricts for privacy reasons.

[^1]: Each attachment becomes part of a MIME multipart message, and is identified by its Content-ID. To refer to that part, RazorMail then uses the cid:(Content-ID) URI scheme.

Or, to attach a file:

@{
    AttachFile("Invoice.pdf", someByteArray);
}

(Because file attachments don't relate to the body, you probably want to put this near the Subject.)

Sending

Depending on which NuGet package you've picked above, you get a backend for sending either via the classic System.Net.Mail, or via MailKit/MimeKit. In the code below, replace YourMailSender with either SystemNetMailSender or MailKitMailSender.

To wrap it all up:

const string view = "Sample";
// this is your model. If you're sending to multiple people, you may want to customize this per person.
var model = new SampleModel(request.Salutation);
// this renders your view (your e-mail template), with the above model as an argument).
var renderedMail = await EmailRenderer.RenderAsync(view, model);

// this wraps the rendered HTML and passes it to a service that handles sending.
// You need someone to send from, and one or more recipients.
var mail = new YourMailSender(from: request.From,
                              to: new[] { (MailAddress)request.To }.ToList(),
                              renderedMail);

// this actually sends the e-mail
await mail.SendAsync(SmtpAccount);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
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
2.2.0 88 10/14/2024
2.1.1 88 10/14/2024

# 2.2.0
- Added some badges to the README
- Marked `AddRazorEmailRenderer` obsolete, use `AddRazorMailRenderer` instead
- Marked `AddMailKitRazorMailClient` obsolete, use `AddMailKitMailClient` instead
- Marked `AddSystemNetRazorMailClient` obsolete, use `AddSystemNetMailClient` instead
- Added `configureClient` parameter to `AddMailKitMailClient` and `AddSystemNetMailClient` so you can configure the
client instance (e.g. default headers)
# 2.1.1
- Initial nuget.org release
- No feature changes, just a version bump