TRENZ.Lib.RazorMail.MailKit
2.3.0
dotnet add package TRENZ.Lib.RazorMail.MailKit --version 2.3.0
NuGet\Install-Package TRENZ.Lib.RazorMail.MailKit -Version 2.3.0
<PackageReference Include="TRENZ.Lib.RazorMail.MailKit" Version="2.3.0" />
<PackageVersion Include="TRENZ.Lib.RazorMail.MailKit" Version="2.3.0" />
<PackageReference Include="TRENZ.Lib.RazorMail.MailKit" />
paket add TRENZ.Lib.RazorMail.MailKit --version 2.3.0
#r "nuget: TRENZ.Lib.RazorMail.MailKit, 2.3.0"
#:package TRENZ.Lib.RazorMail.MailKit@2.3.0
#addin nuget:?package=TRENZ.Lib.RazorMail.MailKit&version=2.3.0
#tool nuget:?package=TRENZ.Lib.RazorMail.MailKit&version=2.3.0
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
You currently need to create an ASP.NET app to use the razor mail renderer. See #7 for more information.
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.
We recommend leveraging Microsoft.Extensions.DependencyInjection
, by calling the following convenience methods:
services.AddRazorMailRenderer();
services.AddMailKitMailClient();
// or
services.AddSystemNetMailClient();
You can also pass a callback to the mail clients to configure default headers (setting a global From
header, for example).
The IMailRenderer
is a scoped service.
This means you will either need to register your service as a scoped service or obtain a scope using this code:
await using var scope = serviceProvider.CreateScopeAsync();
var renderer = scope.ServiceProvider.GetRequiredService<IMailRenderer>();
This is how you would actually render and send an e-mail:
const string viewName = "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).
IMailRenderer renderer = ...; // inject via DI, for example
var content = await renderer.RenderAsync(viewName, 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 MailMessage
{
Headers = new()
{
Recipients = [request.To],
// here you can add CC, BCC and other headers
},
Content = content,
};
// this actually sends the e-mail
IMailClient client = ...; // inject via DI or use MailKitMailClient or SystemNetMailClient directly
await client.SendAsync(mail);
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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- JetBrains.Annotations (>= 2024.2.0)
- MailKit (>= 4.8.0)
- TRENZ.Lib.RazorMail.Core (>= 2.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
# 2.3.0
- Added support for setting a message importance (`Importance`, `X-Priority`) [#26](https://github.com/trenz-gmbh/TRENZ.Lib.RazorMail/pull/26) by @chucker
- Fixed a NRE when sending mails without attachments [#29](https://github.com/trenz-gmbh/TRENZ.Lib.RazorMail/pull/30) by @ricardoboss
# 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