MailFusion 1.0.0
See the version list below for details.
dotnet add package MailFusion --version 1.0.0
NuGet\Install-Package MailFusion -Version 1.0.0
<PackageReference Include="MailFusion" Version="1.0.0" />
paket add MailFusion --version 1.0.0
#r "nuget: MailFusion, 1.0.0"
// Install MailFusion as a Cake Addin #addin nuget:?package=MailFusion&version=1.0.0 // Install MailFusion as a Cake Tool #tool nuget:?package=MailFusion&version=1.0.0
MailFusion
<img src="https://raw.githubusercontent.com/ahmedkamalio/DotNet.MailFusion/main/icon.png" alt="MailFusion Icon" width="100" height="100">
A modern, flexible email delivery library for .NET that simplifies sending emails through various providers like SendGrid and Amazon SES. MailFusion offers robust template support, comprehensive error handling, and a clean, strongly-typed API.
Features
- ๐ง Multiple Email Provider Support
- SendGrid
- Amazon SES
- Development (console output for testing)
- ๐ Template Support
- Scriban template engine integration
- HTML and plain text support
- Template caching for performance
- Strong typing for template models
- โก Modern .NET Features
- Async/await throughout
- Nullable reference types
- Record types for immutable data
- Modern C# features
- ๐ก๏ธ Robust Error Handling
- Detailed error information
- Provider-specific error mapping
- Strongly-typed error codes
- Comprehensive logging
- ๐งช Development-Friendly
- Development provider for testing
- Detailed debugging information
- Comprehensive XML documentation
- Rich logging support
Installation
Install MailFusion via NuGet:
dotnet add package MailFusion
Quick Start
1. Configure the Email Service
In your appsettings.json
:
{
"Email": {
"Provider": "SendGrid",
"SendGrid": {
"ApiKey": "your-api-key"
}
}
}
2. Register Services
In your Program.cs
or Startup.cs
:
services.AddConfiguredEmailService(Configuration, Environment);
3. Create an Email Template Model
public class WelcomeEmailModel : IEmailTemplateModel
{
public string Subject => "Welcome to Our Service!";
public string Email { get; init; }
public string UserName { get; init; }
}
4. Send an Email
public class EmailService
{
private readonly IEmailService _emailService;
public EmailService(IEmailService emailService)
{
_emailService = emailService;
}
public async Task SendWelcomeEmailAsync(string userEmail, string userName)
{
var model = new WelcomeEmailModel
{
Email = userEmail,
UserName = userName
};
var sender = new EmailSender
{
Name = "My Service",
Email = "noreply@myservice.com",
ReplyEmail = "support@myservice.com"
};
var recipients = new[]
{
new EmailRecipient { Email = userEmail, Name = userName }
};
var result = await _emailService.SendFromTemplateAsync(
"welcome-email",
model,
sender,
recipients);
if (result.IsFailure)
{
// Handle error
}
}
}
Provider Configuration
SendGrid
{
"Email": {
"Provider": "SendGrid",
"SendGrid": {
"ApiKey": "your-api-key"
}
}
}
Amazon SES
{
"Email": {
"Provider": "AmazonSes",
"AmazonSes": {
"AccessKey": "your-access-key",
"SecretKey": "your-secret-key",
"Region": "us-east-1"
}
}
}
Development Provider
{
"Email": {
"Provider": "Development",
"Development": {
"UseColors": true,
"ShowHtmlBody": true,
"ShowPlainTextBody": false
}
}
}
Template System
MailFusion uses Scriban for template processing. Templates should be organized in your project as follows:
Templates/
โโโ Email/
โโโ welcome/
โ โโโ welcome.html
โ โโโ welcome.txt
โโโ order-confirmation/
โโโ order-confirmation.html
โโโ order-confirmation.txt
Configure template location in appsettings.json
:
{
"EmailTemplates": {
"Provider": "File",
"File": {
"TemplatesPath": "Templates/Email"
}
}
}
Example Template
HTML template (welcome.html
):
<h1>Welcome {{ UserName }}!</h1>
<p>Thank you for joining our service.</p>
Text template (welcome.txt
):
Welcome {{ UserName }}!
Thank you for joining our service.
Error Handling
MailFusion uses the Result pattern for error handling:
var result = await _emailService.SendFromTemplateAsync(...);
if (result.IsFailure)
{
var error = result.Error;
logger.LogError("Failed to send email: {ErrorCode} - {ErrorMessage}",
error.Code,
error.Message);
}
Development and Testing
For development and testing, use the Development provider:
{
"Email": {
"Provider": "Development"
}
}
This will output emails to the console instead of sending them.
Logging
MailFusion integrates with Microsoft.Extensions.Logging:
services.AddLogging(builder =>
{
builder.AddConsole();
// Add other logging providers as needed
});
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- SendGrid - Email service provider
- Amazon SES - Email service provider
- Scriban - Template engine
Support
For support, please open an issue in the GitHub repository.
Made with โค๏ธ by Ahmed Kamal
Product | Versions 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. |
-
net8.0
- AWSSDK.SimpleEmail (>= 3.7.401.40)
- ResultObject (>= 1.1.3)
- Scriban (>= 5.12.0)
- SendGrid (>= 9.29.3)
-
net9.0
- AWSSDK.SimpleEmail (>= 3.7.401.40)
- ResultObject (>= 1.1.3)
- Scriban (>= 5.12.0)
- SendGrid (>= 9.29.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.