CodeSwitch.Utils.EmailQueuer 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package CodeSwitch.Utils.EmailQueuer --version 1.0.1
NuGet\Install-Package CodeSwitch.Utils.EmailQueuer -Version 1.0.1
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="CodeSwitch.Utils.EmailQueuer" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CodeSwitch.Utils.EmailQueuer --version 1.0.1
#r "nuget: CodeSwitch.Utils.EmailQueuer, 1.0.1"
#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 CodeSwitch.Utils.EmailQueuer as a Cake Addin
#addin nuget:?package=CodeSwitch.Utils.EmailQueuer&version=1.0.1

// Install CodeSwitch.Utils.EmailQueuer as a Cake Tool
#tool nuget:?package=CodeSwitch.Utils.EmailQueuer&version=1.0.1

Email Queuer

A library for asp.net core that allows you to send emails without having memory leaks and in a very clean way. This library can do the following:

  • Send emails in the background without risking a Memory crash
  • Saves the status of each email so you can resend emails that encountered errors while sending
  • Use razor templating engine (RazorLight)
  • Move global css inline to support email styling without the headache of inline styles.

How to use

Follow these steps to get your email queuer up and running

Install package from Nuget

Run the following command in your package manager console: Install-Package CodeSwitch.Utils.EmailQueuer

Add your Razor templates

Create a folder for your templates, then create an empty class in that folder, let's call it Mails.cs Add your .cshtml files to this folder (you can also put them in subfolders) Add the following to your .csproj:

  <PropertyGroup>
    
    <PreserveCompilationReferences>true</PreserveCompilationReferences>
  </PropertyGroup>
  
  
  
  <ItemGroup>
    <EmbeddedResource Include="FolderContainingRazorTemplates\**" />
  </ItemGroup>
Add database table

The email queuer main objective is to avoid memory leaks, so the email queue should be stored in the database, so your DbContext should implement IEmailQueuerContext like the following:

public class AppDbContext : DbContext, IEmailQueuerContext
{
    public AppDbContext(DbContextOptions options) : base(options) { }
   
    // Add the required table
    public virtual DbSet<EmailQueuerTask> EmailQueuerTasks { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        
        // This currently doesn't do much but but may be required in future versions so make sure to call it
        (this as IEmailQueuerContext).Initialize(modelBuilder);
    }
}
Adding service to the dependency inejction container

Now it's time to edit the Startup.cs: In the ConfigureServices method add the following:

services.AddEmailQueuer<AppDbContext>(typeof(Mail), options =>
{
    // default section name is "EmailQueuer"
    options.LoadFromConfiguration(configuration /*, Section name in appsettings.json */);
});

This is how your appsettings.json should look like, replace <SectionName> with the one you used above:

"<SectionName>": {
    "Sender": {
      "Email": "sender@example.com",
      "Password": "123456"
    },
    "Smtp": {
      "Timeout": 20,
      "Host": "smtp.gmail.com",
      "Port":  587
    },
    "ViewBag": {
      "WebsiteLink": "https://github.com/omneimneh/email-queuer"
    },
    "MoveCssInline": true
  }

You can also configure them manually Note that it's not recommended to put your credentials in the source code

options.Sender.Password = "123456";
options.SmtpClient.Host = "smtp.gmail.com";
options.SmtpClient.Port = 587;
options.SmtpClient.EnableSsl = true;
options.SmtpClient.Timeout = 20;
options.ViewBag.WebsiteLink = "https://github.com/omneimneh/email-queuer";
options.MoveCssInline = true;
Use the email sender in your services or controllers

You're basically ready to go, all you need to do is inject the EmailQueuer in your service ro controller.

private readonly EmailQueuer<AppDbContext> emailQueuer;

public HomeController(EmailQueuer<AppDbContext> emailQueuer)
{
    this.emailQueuer = emailQueuer;
}

Now call the EnqueueAsync method with the following args:

  • Receiver email, or ; seperated list of emails, or array of emails
  • Subject of the email
  • Email template name, for example "Welcome", Note that this will be replaced by: string.Format(TemplatePath, "Welcome"), your template path is by default {0}.cshtml, you can also configure it in the startup AddEmailQueuer extenison method
  • The model, it is important that it can be serializable and using the same class used in the Razor template file Welcome.cshtml.
await emailQueuer.EnqueueAsync(email, "Example", "Welcome", new Person
{
    FirstName = "FName",
    LastName = "LName"
});

And congrats, your email will be sent in the background with no additional effort.

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 netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.0.0 223 9/27/2023
1.0.5.3 144 9/26/2023
1.0.5.2 625 3/18/2021
1.0.5.1 360 3/18/2021
1.0.5 295 3/18/2021
1.0.4 473 6/2/2020
1.0.3 445 6/2/2020
1.0.2 427 6/2/2020
1.0.1 446 6/2/2020
1.0.0 439 4/23/2020

Fixed ciritical issue. The package now works as intended.