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:
There is a blog post which extensively describes the approach and background of this library available over here.
The NuGet hosted package is available.
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:
Microsoft.NET.Sdk.Razor
.AddRazorSupportForMvc
element 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.
Skyhop.Mail
project to your template project..AddMailDispatcher()
extension method on your IServiceCollection
as 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");
});
IMailSender
to your IServiceCollection
. In the example project a simple Smtp implementation is included.MailDispatcher
instance. 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") });
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());
The following limitations are currently available. Feel free to submit a PR to fix one or more of those ☺.
netcoreapp3.1
. This is a limitation based on the requirements of the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
dependency.