Welcome to the official Mailtrap .NET Client repository.
This client allows you to quickly and easily integrate your .NET application with v2.0 of the Mailtrap API.
To get the most out of this official Mailtrap.io .NET SDK:
- Create a Mailtrap account
- Verify your domain
- Obtain API token
- Please ensure your project targets .NET implementation which supports .NET Standard 2.0 specification.
The Mailtrap .NET client provides comprehensive access to the Mailtrap API v2.0, including:
- Send an email (Transactional and Bulk streams)
- Send an email with a template
- Send emails with attachments
- Send an email
- Send an email with a template
- Message management
- List inboxes
- Get inbox details
- Update inbox settings
- Create, read, update, and delete projects
- Project configuration and settings
- Account access management
- Permissions management
- List accounts you have access to
- Billing information and usage statistics
- Domain verification
The following few simple steps will bring Mailtrap API functionality into your .NET project.
The Mailtrap .NET client packages are available through GitHub Packages.
First, add the GitHub Packages source to your NuGet configuration:
dotnet nuget add source https://nuget.pkg.github.com/railsware/index.json --name github-railsware
Then add Mailtrap package:
dotnet add package Mailtrap -v 2.0.0 -s github-railsware
Optionally, you can add Mailtrap.Abstractions package:
dotnet add package Mailtrap.Abstractions -v 2.0.0 -s github-railsware
Add Mailtrap services to the DI container.
using Mailtrap;
...
hostBuilder.ConfigureServices((context, services) =>
{
services.AddMailtrapClient(options =>
{
// Definitely, hardcoding a token isn't a good idea.
// This example uses it for simplicity, but in real-world scenarios
// you should consider more secure approaches for storing secrets.
// Environment variables can be an option, as well as other solutions:
// https://learn.microsoft.com/aspnet/core/security/app-secrets
// or https://learn.microsoft.com/aspnet/core/security/key-vault-configuration
options.ApiToken = "<API_TOKEN>";
});
});
Now you can inject IMailtrapClient
instance in any application service and use it to make API calls.
using Mailtrap;
using Mailtrap.Emails.Requests;
using Mailtrap.Emails.Responses;
namespace MyAwesomeProject;
public sealed class SendEmailService : ISendEmailService
{
private readonly IMailtrapClient _mailtrapClient;
public SendEmailService(IMailtrapClient mailtrapClient)
{
_mailtrapClient = mailtrapClient;
}
public async Task DoWork()
{
try
{
SendEmailRequest request = SendEmailRequest
.Create()
.From("[email protected]", "John Doe")
.To("[email protected]")
.Subject("Invitation to Earth")
.Text("Dear Bill,\n\nIt will be a great pleasure to see you on our blue planet next weekend.\n\nBest regards, John.");
SendEmailResponse response = await _mailtrapClient
.Email()
.Send(request);
string messageId = response.MessageIds.FirstOrDefault();
}
catch (MailtrapException mtex)
{
// handle Mailtrap API specific exceptions
}
catch (OperationCancelledException ocex)
{
// handle cancellation
}
catch (Exception ex)
{
// handle other exceptions
}
}
}
The repository includes comprehensive examples demonstrating various use cases and features:
- Email Sending - Send an email (Transactional and Bulk streams)
- Email Sending - Send an email with a template
- Email Sending - Send a batch of emails (Transactional and Bulk streams)
- Email Sending - Send emails with attachments
- Email Sending - Send an email
- Email Sending - Send an email with a template
- Testing Messages - Message management
- Attachments - Working with email attachments in testing messages
- Inbox Management - Inbox management
- Project Management - Project management
- Account Access - Account access management
- Permissions - Permissions management
- Account Management - List accounts you have access to
- Billing - Billing information and usage statistics
- Sending Domains - Domain verification
- Comprehensive API Usage - Complete example showcasing multiple API features together
- Dependency Injection - Integration with ASP.NET Core DI container and configuration
- Factory Pattern - Using standalone client factory for scenarios without DI container
Each example includes detailed comments and demonstrates best practices for error handling, configuration, and resource management.
Please visit Documentation Portal for detailed setup, configuration and usage instructions.
We believe in the power of OSS and welcome any contributions to the library.
Please refer to Contributing Guide for details.
Licensed under the MIT License.