Skyhop.Mail
2.0.0
See the version list below for details.
dotnet add package Skyhop.Mail --version 2.0.0
NuGet\Install-Package Skyhop.Mail -Version 2.0.0
<PackageReference Include="Skyhop.Mail" Version="2.0.0" />
<PackageVersion Include="Skyhop.Mail" Version="2.0.0" />
<PackageReference Include="Skyhop.Mail" />
paket add Skyhop.Mail --version 2.0.0
#r "nuget: Skyhop.Mail, 2.0.0"
#:package Skyhop.Mail@2.0.0
#addin nuget:?package=Skyhop.Mail&version=2.0.0
#tool nuget:?package=Skyhop.Mail&version=2.0.0
<a href="https://skyhop.org"><img src="https://app.skyhop.org/assets/images/skyhop.svg" width=200 alt="skyhop logo" /></a>
This .NET Core 3.1 library is an abstraction of the approach we use over at Skyhop to send transactional emails to our subscribers. We believe that having a flexible method is vital to the ability to flexibly send emails.
This core principle behind this library is based on several prior blog posts:
- Rendering Razor views by supplying a model instance
- Using the RazorViewToStringRenderer with Asp.Net Core 3
There is a blog post which extensively describes the approach and background of this library available over here.
Installation
The NuGet hosted package is available over here. Install it using the following commands:
Using the NuGet Package Manager
Install-Package Skyhop.Mail
Using the .NET CLI
dotnet add package Skyhop.Mail
You should reference this project on your template project (or at least the project which contains your views and view-models). After which there are two changes you will need to make to your .csproj file:
- Set the SDK target to
Microsoft.NET.Sdk.Razor. - Make sure you add the
AddRazorSupportForMvcelement to the file so that it looks like this:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
Samples which can be used as a reference can be found in this repository.
Usage
- Add the
Skyhop.Mailproject to your template project. - Use the
.AddMailDispatcher()extension method on yourIServiceCollectionas follows. Note that this library expects you to bring your own transport mechanism.
services.AddMailDispatcher(builder =>
{
builder.DefaultFromAddress = new MimeKit.MailboxAddress("Email Support", "support@example.tld");
});
- Add an implementation of
IMailSenderto yourIServiceCollection. In the example project a simple Smtp implementation is included. - Add views and viewmodels to your templating project.
- Use the DI container to grab a
MailDispatcherinstance. Usage from code can be as follows. After this you can send an email based on the viewmodel as follows:
await _mailDispatcher.SendMail(
data: new ServiceActionModel
{
ActionName = "Starting",
Timestamp = DateTime.UtcNow
},
to: new[] { new MailboxAddress("John Doe", "john.doe@example.tld") });
Convention based view loading
If you would like to load all views in *.Views.dll's you can use an overload AddMailDispatcher, this overload enables the extension of IMvcCoreBuilder. We created an extension which will find and load all *.Views.dll files as application parts:
services.AddMailDispatcher(options =>
{
options.DefaultFromAddress = new MailboxAddress("Email Support", "support@example.tld");
},
builder => builder.AddViewsApplicationParts());
Gotchas
The following limitations are currently available. Feel free to submit a PR to fix one or more of those ☺.
- This library only works with projects which target
netcoreapp3.1. This is a limitation based on the requirements of theMicrosoft.AspNetCore.Mvc.Razor.RuntimeCompilationdependency. - It is expected that a view-model is only used once within a view. The code will use the first view it encounters that has the chosen model.
- If your implementation of
IMailSenderis scoped (uses aDbContextfor example), you can also change the scope of theMailDispatcheras needed. By default theMailDispatcheris added as a singleton, but using an overload of theAddMailDispatcheryou can set theServiceLifetimeas needed.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
| .NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- HtmlAgilityPack (>= 1.11.17)
- MailKit (>= 2.4.1)
- Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation (>= 3.1.0)
- PreMailer.Net (>= 2.1.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- Add bug fixed which resulted in email having no body,
- Removed dependency on WebHostBuilder
- Fixed a typo in AddMailDispatcher
- Removed the need to load all assemblies when trying to find a model/identifier combination
- Made MailSender awaitable
- Moved MailDispatcherOptions to options folder
- Moved IModelIdentifierLister to Abstractions folder
- Refactored the MailSender delegate to an interface, so now DI can be used for the send part