Skip to content

railsware/mailtrap-dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Mailtrap

GitHub Package Version CI License

Official Mailtrap .NET Client

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.

Prerequisites

To get the most out of this official Mailtrap.io .NET SDK:

Supported functionality

The Mailtrap .NET client provides comprehensive access to the Mailtrap API v2.0, including:

Email API/SMTP

  • Send an email (Transactional and Bulk streams)
  • Send an email with a template
  • Send emails with attachments

Email Sandbox (Testing)

  • 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

General

  • Account access management
  • Permissions management
  • List accounts you have access to
  • Billing information and usage statistics
  • Domain verification

Quick Start

The following few simple steps will bring Mailtrap API functionality into your .NET project.

Install

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

Configure

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>";
   });
});   

Use

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
        }   
    }
}

Examples

The repository includes comprehensive examples demonstrating various use cases and features:

Email API/SMTP

Email Sandbox (Testing)

General

Configuration & Setup

Each example includes detailed comments and demonstrates best practices for error handling, configuration, and resource management.

Documentation

Please visit Documentation Portal for detailed setup, configuration and usage instructions.

Contributing

We believe in the power of OSS and welcome any contributions to the library.
Please refer to Contributing Guide for details.

License

Licensed under the MIT License.

About

Official mailtrap.io .NET client

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages