Govindkm.DotMailer.Extensions.DependencyInjection 1.0.0-beta.7

This is a prerelease version of Govindkm.DotMailer.Extensions.DependencyInjection.
dotnet add package Govindkm.DotMailer.Extensions.DependencyInjection --version 1.0.0-beta.7
                    
NuGet\Install-Package Govindkm.DotMailer.Extensions.DependencyInjection -Version 1.0.0-beta.7
                    
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="Govindkm.DotMailer.Extensions.DependencyInjection" Version="1.0.0-beta.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Govindkm.DotMailer.Extensions.DependencyInjection" Version="1.0.0-beta.7" />
                    
Directory.Packages.props
<PackageReference Include="Govindkm.DotMailer.Extensions.DependencyInjection" />
                    
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 Govindkm.DotMailer.Extensions.DependencyInjection --version 1.0.0-beta.7
                    
#r "nuget: Govindkm.DotMailer.Extensions.DependencyInjection, 1.0.0-beta.7"
                    
#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 Govindkm.DotMailer.Extensions.DependencyInjection@1.0.0-beta.7
                    
#: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=Govindkm.DotMailer.Extensions.DependencyInjection&version=1.0.0-beta.7&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Govindkm.DotMailer.Extensions.DependencyInjection&version=1.0.0-beta.7&prerelease
                    
Install as a Cake Tool

Govindkm.DotMailer.Extensions.DependencyInjection

Dependency injection extensions for DotMailer. Simplifies registration of email services in ASP.NET Core and .NET applications.

Overview

This package provides convenient extension methods for the Microsoft.Extensions.DependencyInjection container, allowing you to set up DotMailer with a single method call.

Features:

  • ✅ One-line service registration
  • ✅ Configuration binding support (appsettings.json)
  • ✅ Automatic SMTP transport setup
  • ✅ Logging integration
  • ✅ Type-safe configuration

Installation

dotnet add package Govindkm.DotMailer.Extensions.DependencyInjection

Also install the core packages:

dotnet add package Govindkm.DotMailer.Core
dotnet add package Govindkm.DotMailer.SMTP

Quick Start

appsettings.json:

{
  "SmtpSettings": {
    "Host": "smtp.gmail.com",
    "Port": 587,
    "UseSsl": false,
    "UseStartTls": true,
    "Username": "your-email@gmail.com",
    "Password": "your-app-password"
  }
}

Program.cs:

using Govindkm.DotMailer.Extensions.DependencyInjection;

var builder = WebApplicationBuilder.CreateBuilder(args);

// Add DotMailer with automatic configuration from appsettings.json
builder.Services.AddDotMailer();

var app = builder.Build();
app.Run();

Option 2: Programmatic Configuration

using Govindkm.DotMailer.Extensions.DependencyInjection;

var builder = WebApplicationBuilder.CreateBuilder(args);

builder.Services.AddDotMailer(options =>
{
    options.Host = "smtp.gmail.com";
    options.Port = 587;
    options.UseStartTls = true;
    options.Username = "your-email@gmail.com";
    options.Password = "your-app-password";
});

var app = builder.Build();

Option 3: Configuration Section Name

// Use a custom configuration section (default is "SmtpSettings")
builder.Services.AddDotMailer(builder.Configuration, "CustomSmtpSection");

Usage

Once registered, inject IEmailClient into your services:

using Govindkm.DotMailer.Core;

public class WelcomeEmailService
{
    private readonly IEmailClient _emailClient;

    public WelcomeEmailService(IEmailClient emailClient)
    {
        _emailClient = emailClient;
    }

    public async Task SendWelcomeEmail(string email, string name)
    {
        var message = new EmailMessage
        {
            From = new EmailAddress("noreply@example.com", "Welcome"),
            To = new[] { new EmailAddress(email, name) },
            Subject = "Welcome to our service!",
            HtmlBody = $"<h1>Hello {name}!</h1><p>Thank you for joining us.</p>",
            TextBody = $"Hello {name}!\n\nThank you for joining us."
        };

        var result = await _emailClient.SendAsync(message);
        
        return result.IsSuccess 
            ? $"Welcome email sent (ID: {result.MessageId})"
            : $"Failed to send: {result.ErrorMessage}";
    }
}

In a Controller

using Microsoft.AspNetCore.Mvc;
using Govindkm.DotMailer.Core;

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

    public EmailController(IEmailClient emailClient)
    {
        _emailClient = emailClient;
    }

    [HttpPost("send")]
    public async Task<IActionResult> SendEmail([FromBody] EmailRequest request)
    {
        var message = new EmailMessage
        {
            From = new EmailAddress("noreply@example.com"),
            Subject = request.Subject,
            HtmlBody = request.Body
        };

        message.To.Add(new EmailAddress(request.To));

        var result = await _emailClient.SendAsync(message);

        if (!result.IsSuccess)
            return BadRequest(new { error = result.ErrorMessage });

        return Ok(new { messageId = result.MessageId });
    }
}

Configuration Options

The AddDotMailer() method accepts these configuration options:

Option Type Required Default Description
Host string ✅ — SMTP server hostname
Port int ❌ 587 SMTP server port
Username string ✅ — SMTP authentication username
Password string ✅ — SMTP authentication password
UseSsl bool ❌ false Use SSL/TLS from start (port 465)
UseStartTls bool ❌ true Upgrade to TLS (port 587)
TimeoutMs int ❌ 30000 Connection timeout in milliseconds

Common SMTP Configurations

Gmail

{
  "SmtpSettings": {
    "Host": "smtp.gmail.com",
    "Port": 587,
    "UseStartTls": true,
    "Username": "your-email@gmail.com",
    "Password": "your-app-password"
  }
}

SendGrid

{
  "SmtpSettings": {
    "Host": "smtp.sendgrid.net",
    "Port": 587,
    "UseStartTls": true,
    "Username": "apikey",
    "Password": "SG.xxxxxxxxxxxxx"
  }
}

AWS SES

{
  "SmtpSettings": {
    "Host": "email-smtp.us-east-1.amazonaws.com",
    "Port": 587,
    "UseStartTls": true,
    "Username": "your-smtp-username",
    "Password": "your-smtp-password"
  }
}

Logging

Enable logging to see DotMailer operations:

builder.Logging.AddConsole();
builder.Logging.SetMinimumLevel(LogLevel.Debug);  // See detailed SMTP operations

Error Handling

var result = await _emailClient.SendAsync(message);

if (!result.IsSuccess)
{
    _logger.LogError("Email send failed: {Error}", result.ErrorMessage);
    
    // Handle specific errors
    if (result.ErrorMessage.Contains("authentication"))
        return StatusCode(500, "Email service configuration error");
    
    return StatusCode(500, "Failed to send email");
}

_logger.LogInformation("Email sent successfully: {MessageId}", result.MessageId);

Security Best Practices

  1. Never hardcode credentials - Use configuration files or environment variables
  2. Use secrets manager in production:
    builder.Configuration.AddUserSecrets<Program>();
    
  3. Use app-specific passwords for Gmail
  4. Always use TLS/SSL encryption
  5. Validate email addresses before sending
  6. Rate limit to prevent abuse

Environment Variables

# Linux/macOS
export SmtpSettings__Username="your-email@gmail.com"
export SmtpSettings__Password="your-app-password"

# Windows (PowerShell)
$env:SmtpSettings__Username="your-email@gmail.com"
$env:SmtpSettings__Password="your-app-password"

Then in your code:

builder.Configuration.AddEnvironmentVariables();

User Secrets (Development)

# Initialize user secrets
dotnet user-secrets init

# Set values
dotnet user-secrets set "SmtpSettings:Username" "your-email@gmail.com"
dotnet user-secrets set "SmtpSettings:Password" "your-app-password"

# View all secrets
dotnet user-secrets list

Multiple Email Configurations

If you need multiple email configurations:

// Option 1: Register with different configuration sections
services.Configure<SmtpTransportOptions>(
    "Gmail", 
    configuration.GetSection("Smtp:Gmail")
);

services.Configure<SmtpTransportOptions>(
    "SendGrid", 
    configuration.GetSection("Smtp:SendGrid")
);

// Option 2: Use factory pattern to create different clients
services.AddScoped<IGmailEmailClient>(sp => 
    new SmtpEmailClient(sp.GetRequiredService<IOptions<SmtpTransportOptions>>().Value)
);

What Gets Registered

When you call AddDotMailer(), the following services are registered:

  • IEmailClient → DefaultEmailClient (Scoped)
  • IEmailTransport → SmtpEmailTransport (Scoped)
  • SmtpTransportOptions → From configuration (Singleton)
  • Logging support for all services

Troubleshooting

Issue: "No 'SmtpSettings' configuration section found"

  • Solution: Ensure appsettings.json has the SmtpSettings section, or use AddDotMailer(config, "CustomSection")

Issue: "Failed to inject IEmailClient"

  • Solution: Call AddDotMailer() before building the service provider

Issue: "Authentication failed"

  • Solution: Verify credentials in configuration, check if app-specific password is needed (Gmail)

Issue: "Connection timeout"

  • Solution: Check Host and Port values, verify firewall allows outbound SMTP connections

More Information

For complete documentation and examples: DotMailer GitHub Repository

License

MIT License - See LICENSE file in repository for details

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.0-beta.7 79 7/14/2026
1.0.0-beta.6 76 7/14/2026