Linger.Email.AspNetCore 0.7.2

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Linger.Email.AspNetCore --version 0.7.2
                    
NuGet\Install-Package Linger.Email.AspNetCore -Version 0.7.2
                    
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="Linger.Email.AspNetCore" Version="0.7.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Linger.Email.AspNetCore" Version="0.7.2" />
                    
Directory.Packages.props
<PackageReference Include="Linger.Email.AspNetCore" />
                    
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 Linger.Email.AspNetCore --version 0.7.2
                    
#r "nuget: Linger.Email.AspNetCore, 0.7.2"
                    
#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 Linger.Email.AspNetCore@0.7.2
                    
#: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=Linger.Email.AspNetCore&version=0.7.2
                    
Install as a Cake Addin
#tool nuget:?package=Linger.Email.AspNetCore&version=0.7.2
                    
Install as a Cake Tool

Linger.Email.AspNetCore

πŸ“ View this document in: English | δΈ­ζ–‡

Overview

Linger.Email.AspNetCore provides seamless ASP.NET Core integration for the Linger.Email library. It simplifies email service configuration through dependency injection, configuration binding, and provides enhanced email service capabilities specifically designed for modern ASP.NET Core applications.

Installation

dotnet add package Linger.Email.AspNetCore

Features

  • Dependency Injection Integration: Simple service registration and configuration
  • Configuration Binding: Automatic binding from appsettings.json
  • Enhanced Email Service: Additional convenience methods for common email scenarios
  • Logging Integration: Built-in logging support for email operations
  • Configuration Validation: Automatic validation of email configuration
  • Cross-platform: Supports .NET 9.0 and .NET 8.0

Quick Start

1. Configure Email Settings

Add email configuration to your appsettings.json:

{
  "EmailConfig": {
    "Host": "smtp.gmail.com",
    "Port": 587,
    "UseSsl": true,
    "UseStartTls": true,
    "UserName": "your-email@gmail.com",
    "Password": "your-app-password",
    "From": {
      "Address": "your-email@gmail.com",
      "Name": "Your Application"
    },
    "Bcc": [
      {
        "Address": "audit@yourcompany.com",
        "Name": "Audit Trail"
      }
    ]
  }
}

2. Register Email Services

In your Program.cs (or Startup.cs for older versions):

using Linger.Email.AspNetCore;

var builder = WebApplication.CreateBuilder(args);

// Method 1: Simple configuration binding
builder.Services.ConfigureEmail(builder.Configuration);

// Method 2: Using MailKit configuration (alternative)
// builder.Services.ConfigureMailKit(builder.Configuration);

var app = builder.Build();

3. Use Email Service in Controllers

using Linger.Email.AspNetCore;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class EmailController : ControllerBase
{
    private readonly IEmailService _emailService;

    public EmailController(IEmailService emailService)
    {
        _emailService = emailService;
    }

    [HttpPost("send-text")]
    public async Task<IActionResult> SendTextEmail([FromBody] BasicEmailRequest request)
    {
        await _emailService.SendTextEmailAsync(
            request.To, 
            request.Subject, 
            request.Body
        );

        return Ok("Text email sent successfully");
    }

    [HttpPost("send-html")]
    public async Task<IActionResult> SendHtmlEmail([FromBody] BasicEmailRequest request)
    {
        await _emailService.SendHtmlEmailAsync(
            request.To, 
            request.Subject, 
            request.Body
        );

        return Ok("HTML email sent successfully");
    }

    [HttpPost("send-with-attachments")]
    public async Task<IActionResult> SendWithAttachments([FromBody] EmailWithAttachmentsRequest request)
    {
        await _emailService.SendWithAttachmentsAsync(
            request.To,
            request.Subject,
            request.Body,
            request.IsHtml,
            request.AttachmentPaths
        );

        return Ok("Email with attachments sent successfully");
    }
}

public record BasicEmailRequest(string To, string Subject, string Body);
public record EmailWithAttachmentsRequest(string To, string Subject, string Body, bool IsHtml, List<string> AttachmentPaths);

Advanced Usage

Using IEmailService Methods

The IEmailService provides convenient methods for common email scenarios:

public class EmailController : ControllerBase
{
    private readonly IEmailService _emailService;

    public EmailController(IEmailService emailService)
    {
        _emailService = emailService;
    }

    // Send simple text email
    public async Task SendTextEmail()
    {
        await _emailService.SendTextEmailAsync(
            "user@example.com",
            "Plain Text Subject",
            "This is a plain text email body."
        );
    }

    // Send HTML email
    public async Task SendHtmlEmail()
    {
        await _emailService.SendHtmlEmailAsync(
            "user@example.com",
            "HTML Email Subject",
            "<h1>Hello!</h1><p>This is an HTML email.</p>"
        );
    }

    // Send email with attachments
    public async Task SendEmailWithAttachments()
    {
        var attachmentPaths = new[]
        {
            @"C:\temp\report.pdf",
            @"C:\temp\image.jpg"
        };

        await _emailService.SendWithAttachmentsAsync(
            "user@example.com",
            "Email with Attachments",
            "<p>Please find the attached files.</p>",
            isHtml: true,
            attachmentPaths
        );
    }
}

Advanced Email Message Configuration

For more complex scenarios, use the underlying SendAsync method:

public class AdvancedEmailController : ControllerBase
{
    private readonly IEmailService _emailService;

    public AdvancedEmailController(IEmailService emailService)
    {
        _emailService = emailService;
    }

    public async Task SendAdvancedEmail()
    {
        var message = new EmailMessage
        {
            To = new List<EmailAddress>
            {
                new("primary@example.com", "Primary Recipient"),
                new("secondary@example.com", "Secondary Recipient")
            },
            Cc = new List<EmailAddress>
            {
                new("manager@example.com", "Manager")
            },
            Subject = "Project Status Update",
            Body = @"
                <h2>Project Status Report</h2>
                <p>Dear Team,</p>
                <p>Here's the latest status of our project:</p>
                <ul>
                    <li>βœ… Phase 1: Completed</li>
                    <li>πŸ”„ Phase 2: In Progress</li>
                    <li>⏳ Phase 3: Pending</li>
                </ul>
                <p>Best regards,<br>Project Manager</p>
            ",
            IsHtmlBody = true,
            Priority = MessagePriority.High
        };

        await _emailService.SendAsync(message, response =>
        {
            // Optional callback for handling send completion
            Console.WriteLine($"Email sent: {response}");
        });
    }
}

Background Email Service

For sending emails in background tasks:

public class BackgroundEmailService : BackgroundService
{
    private readonly IServiceScopeFactory _scopeFactory;
    private readonly ILogger<BackgroundEmailService> _logger;

    // Important: Use IServiceScopeFactory instead of directly injecting IEmailService
    // Reason: BackgroundService is Singleton, directly injecting Transient/Scoped services causes lifecycle issues
    public BackgroundEmailService(
        IServiceScopeFactory scopeFactory,
        ILogger<BackgroundEmailService> logger)
    {
        _scopeFactory = scopeFactory;
        _logger = logger;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        // Delay 10 seconds after startup to avoid resource competition during application startup
        await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);

        // Create service scope and send email once
        using var scope = _scopeFactory.CreateScope();
        var emailService = scope.ServiceProvider.GetRequiredService<IEmailService>();

        try
        {
            await SendNotificationEmail(emailService);
            _logger.LogInformation("Background email sent successfully");
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to send background email");
        }
        // Task completed, service ends naturally
    }

    private async Task SendNotificationEmail(IEmailService emailService)
    {
        await emailService.SendHtmlEmailAsync(
            "admin@company.com",
            "System Startup Notification",
            @"
            <h2>System Startup Notification</h2>
            <p>Hello,</p>
            <p>The system has started successfully and is now running.</p>
            <p>Startup Time: {DateTime.Now:yyyy-MM-dd HH:mm:ss}</p>
            <p>Best regards,<br>System Administration</p>
            "
        );
    }
}

// Register the background service
builder.Services.AddHostedService<BackgroundEmailService>();

Configuration Options

Complete Configuration Example

{
  "EmailConfig": {
    "Host": "smtp.example.com",
    "Port": 587,
    "UseSsl": true,
    "UseStartTls": true,
    "UserName": "smtp-username",
    "Password": "smtp-password",
    "From": {
      "Address": "noreply@example.com",
      "Name": "My Application"
    },
    "Bcc": [
      {
        "Address": "audit@example.com",
        "Name": "Audit Trail"
      },
      {
        "Address": "backup@example.com",
        "Name": "Email Backup"
      }
    ]
  }
}

Error Handling and Logging

public class EmailController : ControllerBase
{
    private readonly IEmailService _emailService;
    private readonly ILogger<EmailController> _logger;

    public EmailController(IEmailService emailService, ILogger<EmailController> logger)
    {
        _emailService = emailService;
        _logger = logger;
    }

    [HttpPost("send")]
    public async Task<IActionResult> SendEmail([FromBody] EmailRequest request)
    {
        try
        {
            _logger.LogInformation("Sending email to {Email} with subject: {Subject}", 
                request.Email, request.Subject);

            await _emailService.SendHtmlEmailAsync(
                request.Email,
                request.Subject,
                request.Body
            );

            _logger.LogInformation("Email sent successfully to {Email}", request.Email);
            return Ok(new { success = true });
        }
        catch (AuthenticationException ex)
        {
            _logger.LogError(ex, "Email authentication failed");
            return StatusCode(500, new { error = "Email service authentication failed" });
        }
        catch (SmtpException ex)
        {
            _logger.LogError(ex, "SMTP error occurred while sending email");
            return StatusCode(500, new { error = "Email sending failed" });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Unexpected error sending email to {Email}", request.Email);
            return StatusCode(500, new { error = "An unexpected error occurred" });
        }
    }
}

public record EmailRequest(string Email, string Subject, string Body);

Best Practices

  1. Configuration Security: Store email credentials securely using Azure Key Vault or user secrets
  2. Environment-Specific Settings: Use different email configurations for development, staging, and production
  3. Error Handling: Always implement proper exception handling for email operations
  4. Logging: Utilize built-in logging to track email sending operations
  5. Background Processing: For bulk email operations, consider using background services
  6. Resource Management: The service automatically handles resource disposal

Dependencies

  • Linger.Email: Core email functionality
  • Linger.Configuration: Configuration extensions
  • Microsoft.Extensions.Logging.Abstractions: Logging integration
  • Microsoft.Extensions.Options.ConfigurationExtensions: Configuration binding

Supported .NET Versions

  • .NET 9.0
  • .NET 8.0

Core Documentation

πŸ“– For core email functionality and detailed API documentation, see: Linger.Email README

License

This project is licensed under the terms of the license provided with the Linger project.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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 is compatible.  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
0.8.4-preview 211 8/25/2025
0.8.3-preview 111 8/20/2025
0.8.2-preview 171 8/4/2025
0.8.1-preview 116 7/30/2025
0.8.0-preview 494 7/22/2025
0.7.2 148 6/3/2025
0.7.1 152 5/21/2025
0.7.0 149 5/19/2025
0.6.0-alpha 155 4/28/2025
0.5.0-alpha 162 4/10/2025
0.4.0-alpha 151 4/1/2025
0.3.3-alpha 153 3/19/2025
0.3.2-alpha 154 3/17/2025
0.3.1-alpha 143 3/16/2025
0.3.0-alpha 212 3/6/2025
0.2.0-alpha 97 2/9/2025
0.1.2-alpha 99 12/17/2024
0.1.1-alpha 91 12/17/2024
0.1.0-alpha 101 12/6/2024
0.0.4-alpha 93 12/6/2024
0.0.3-alpha 105 11/27/2024
0.0.2-alpha 99 10/3/2024