CPIT.SMTPEmailSender.Core 1.0.1

dotnet add package CPIT.SMTPEmailSender.Core --version 1.0.1
                    
NuGet\Install-Package CPIT.SMTPEmailSender.Core -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="CPIT.SMTPEmailSender.Core" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CPIT.SMTPEmailSender.Core" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="CPIT.SMTPEmailSender.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CPIT.SMTPEmailSender.Core --version 1.0.1
                    
#r "nuget: CPIT.SMTPEmailSender.Core, 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.
#:package CPIT.SMTPEmailSender.Core@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CPIT.SMTPEmailSender.Core&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=CPIT.SMTPEmailSender.Core&version=1.0.1
                    
Install as a Cake Tool

EmailSender

A flexible email sending library for .NET with template support.

Features

  • SMTP email sending with secure connections
  • Multiple email providers
  • Template support (Razor or HTML)
  • Embedded resource templates support
  • Attachment support
  • Error handling with retries
  • DI integration with ASP.NET Core

Installation

dotnet add package CPIT.SMTPEmailSender.Core

Quick Start

Configure in appsettings.json

{
  "SmtpSettings": {
    "Server": "smtp.example.com",
    "EmailAddress": "noreply@example.com",
    "EmailDisplayName": "Application Name",
    "Port": 587,
    "Username": "smtp_username",
    "Password": "smtp_password",
    "SecurityOption": "StartTlsWhenAvailable",
    "LocalDomain": "optional_domain"
  },
  "EmailSender": {
    "AddFooter": true,
    "TemplateLocation": "EmailTemplates"
  }
}

Register services

builder.Services.AddEmailSender(builder.Configuration);

Use in code

public class EmailService
{
    private readonly IEmailSender _emailSender;
    
    public EmailService(IEmailSender emailSender)
    {
        _emailSender = emailSender;
    }
    
    public async Task SendWelcomeEmailAsync(string userEmail, string userName)
    {
        var message = EmailMessage.CreateFromTemplate(
            "WelcomeEmail.cshtml", 
            "Welcome to our application!"
        ) with
        {
            Recipients = new[] { EmailRecipient.Create(userEmail, userName) }
        };
        
        var model = new WelcomeEmailModel
        {
            UserName = userName,
            CurrentDate = DateTime.Now
        };
        
        var options = new EmailSendOptions
        {
            BaseUrl = "https://example.com",
            RetryCount = 3
        };
        
        var result = await _emailSender.SendEmailAsync(message, model, options);
        
        if (!result.IsSuccess)
        {
            Console.WriteLine($"Failed to send email: {result.ErrorMessage}");
        }
    }
}

Template Options

File-based Templates

Templates can be stored in the file system:

// Simple template with extension
var message = EmailMessage.CreateFromTemplate("WelcomeEmail.cshtml", "Welcome!");

// Template in configured template location
var message = EmailMessage.CreateFromTemplate("Newsletter.cshtml", "Newsletter");

Embedded Resource Templates

Templates can also be embedded in assemblies:

// Fully qualified embedded resource path
var message = EmailMessage.CreateFromTemplate(
    "Company.Project.EmailTemplates.WelcomeEmail.cshtml", 
    "Welcome!"
);

To use embedded resources:

  1. Add template files to the project
  2. Set their Build Action to "Embedded Resource"
  3. Reference them using the full namespace path

Advanced Configuration

Builder Pattern

services.AddEmailSender(builder =>
{
    builder.UseSmtp(options =>
    {
        options.Server = "smtp.example.com";
        options.Port = 587;
        options.EmailAddress = "noreply@example.com";
        options.EmailDisplayName = "Application Name";
        options.Username = "smtp_username";
        options.Password = "smtp_password";
    });
    
    builder.Configure(options =>
    {
        options.AddFooter = true;
        options.TemplateLocation = "EmailTemplates";
    });
});

Custom Implementations

services.AddEmailSender(builder =>
{
    builder.UseProvider<CustomEmailProvider>();
    builder.UseTemplateRenderer<CustomTemplateRenderer>();
});

License

This project is licensed under the MIT License.

Product 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. 
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
1.0.1 579 4/9/2025
1.0.0 230 4/9/2025