TRENZ.Lib.RazorMail.Core
2.1.1
See the version list below for details.
dotnet add package TRENZ.Lib.RazorMail.Core --version 2.1.1
NuGet\Install-Package TRENZ.Lib.RazorMail.Core -Version 2.1.1
<PackageReference Include="TRENZ.Lib.RazorMail.Core" Version="2.1.1" />
paket add TRENZ.Lib.RazorMail.Core --version 2.1.1
#r "nuget: TRENZ.Lib.RazorMail.Core, 2.1.1"
// Install TRENZ.Lib.RazorMail.Core as a Cake Addin #addin nuget:?package=TRENZ.Lib.RazorMail.Core&version=2.1.1 // Install TRENZ.Lib.RazorMail.Core as a Cake Tool #tool nuget:?package=TRENZ.Lib.RazorMail.Core&version=2.1.1
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 propertySalutation
, 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 | Versions 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. |
-
net8.0
- NUnit (>= 4.2.2)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on TRENZ.Lib.RazorMail.Core:
Package | Downloads |
---|---|
TRENZ.Lib.RazorMail.MailKit
Write e-mail templates in Razor syntax, and send them via System.Net.Mail or MailKit. |
|
TRENZ.Lib.RazorMail.SystemNet
Write e-mail templates in Razor syntax, and send them via System.Net.Mail or MailKit. |
GitHub repositories
This package is not used by any popular GitHub repositories.
# 2.1.1
- Initial nuget.org release
- No feature changes, just a version bump