Govindkm.DotMailer.Extensions.DependencyInjection
1.0.0-beta.7
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
<PackageReference Include="Govindkm.DotMailer.Extensions.DependencyInjection" Version="1.0.0-beta.7" />
<PackageVersion Include="Govindkm.DotMailer.Extensions.DependencyInjection" Version="1.0.0-beta.7" />
<PackageReference Include="Govindkm.DotMailer.Extensions.DependencyInjection" />
paket add Govindkm.DotMailer.Extensions.DependencyInjection --version 1.0.0-beta.7
#r "nuget: Govindkm.DotMailer.Extensions.DependencyInjection, 1.0.0-beta.7"
#:package Govindkm.DotMailer.Extensions.DependencyInjection@1.0.0-beta.7
#addin nuget:?package=Govindkm.DotMailer.Extensions.DependencyInjection&version=1.0.0-beta.7&prerelease
#tool nuget:?package=Govindkm.DotMailer.Extensions.DependencyInjection&version=1.0.0-beta.7&prerelease
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
Option 1: Configuration from appsettings.json (Recommended)
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
- Never hardcode credentials - Use configuration files or environment variables
- Use secrets manager in production:
builder.Configuration.AddUserSecrets<Program>(); - Use app-specific passwords for Gmail
- Always use TLS/SSL encryption
- Validate email addresses before sending
- 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.jsonhas theSmtpSettingssection, or useAddDotMailer(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 | Versions 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. |
-
net10.0
- Govindkm.DotMailer.Core (>= 1.0.0-beta.7)
- Govindkm.DotMailer.SMTP (>= 1.0.0-beta.7)
- Microsoft.Extensions.Configuration (>= 10.0.9)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.9)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.9)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.9)
- Microsoft.Extensions.Logging (>= 10.0.9)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.9)
- Microsoft.Extensions.Logging.Console (>= 10.0.9)
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 |