Emailer 1.0.5

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

// Install Emailer as a Cake Tool
#tool nuget:?package=Emailer&version=1.0.5                

Welcome to Emailer!

enter image description here

Emailer is a simple service used to generate email contet from razor page and send that content to a recipient via email. This was from personal need of the author to share functionality between projects.

Setup

Emailer consist of two services which must be called in this order.

  1. RazorViewToStringRenderer
  2. Emailer

RazorViewToStringRenderer

This service is responsible for rendering razor view(*.cshtml) to string. Service is added in the ConfigureServices method in following way:

services.AddRazorViewToStringRenderer(options => 
{
	options.ContentRoot = HostingEnvironment.ContentRootPath;
	options.EmailsFolder = "Emails";
});
  • ContentRoot - this option is needed so the service knows where to look for views. It is obtainable via IHostingEnvironment interface.
  • EmailsFolder - this is optional. Default value is "Emails" and it is the folder in Views that holds all the email razor pages.

Emailer

This service is responsible for sending the previously rendered views to the recipients via email. When adding the service there are two ways to define options needed to create the SmtpClient.

1. Via options action

Pass an Action<EmailerOptions> to the method that adds this service:

services.AddEmailer(options => 
{
    options.Port = 587;
    options.Host = "smtp.gmail.com";
    options.EnableSsl = true;
    options.Timeout = 10000;
    options.Sender = "yourmail@gmail.com";
    options.Username = "yourmail@gmail.com";
    options.Password = "passssssss";
});

2. Via IConfiguration

For this to work you have to define a configuration section named SmtpSettings in the appsettings.json:

"SmtpSettings": {
    "Port": 587,
    "Host": "smtp.gmail.com",
    "EnableSsl": true,
    "Timeout": 10000,
    "Sender": "rprimora@gmail.com",
    "Username": "rprimora@gmail.com",
    "Password": "passssssss"
  }

Tip 1: Default value of Timeout is 10000 and if that suits you you do not have to pass it. Tip 2: It is not needed to define Sender if it's same as Username.

The service is than added in following manner:

services.AddEmailer(configuration);

Using the email service

In order to use the email service you have to obtain it via IServiceProvider in following manner.

IServiceProvider.GetService<IEmailer>()

Interface describes two methods:

  • Task SendEmailAsync<TModel>(TModel model) where TModel: EmailModel;
  • Task SendEmailAsync<TModel>(Func<SmtpClient> client, TModel model) where TModel : EmailModel;

Both methods send email that is defined in the given TModel. Second method also accepts a Func<SmtpClient> that returns a SmtpClient which is used to send email. In this case whatever you defined while adding the Email service is ignored.

EmailModel

Base model for email defines some basic information that is needed almost always when sending an email:

  • EmailView - this is the name of the razor page which represents our email body.
  • Subject - email subject.
  • To - email address of the recipient.

If the email has no special changing data(e.g. activation link) than this is enough to send the email. If you have need to send some other information in email than you would inherit EmailModel and define your information there.

Example

Define the activation email model by inheriting EmailModel:

public class ActivationEmailModel : EmailModel
{
	public string ActivationLink { get; set; }
}

Create razor page in Views/Emails and name it ActivationEmail:

@model ActivationEmailModel
<div>
	Thank you for registering with us please activate you account by clicking the link below
	<a href="@Model.ActivationLink">Activate</a>
</div>

Send you email by first obtaining the email service:

var emailService = HttpContext.RequestServices.GetRequiredService<IEmailer>()

Now define your model:

var activationModel = new ActivationEmailModel()
{
	EmailView = "ActivationEmail",
	Subject = "Activate your account",
	To = "recipient@mail.com",
	ActivationLink = "yoursite.com/account/activate?id=abc123"
};

Now send the email:

await emailService.SendEmailAsync(activationModel);
Product 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. 
.NET Core netcoreapp2.2 is compatible.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.2

    • No dependencies.

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
1.0.5 2,136 2/26/2019
1.0.4 1,902 4/23/2018
1.0.3 1,690 4/22/2018
1.0.2 1,630 4/22/2018
1.0.1 1,685 4/22/2018
1.0.0 1,671 4/22/2018