Table of Contents

Configuration Model

Caution

Examples in this article could contain API token specified as a plain text in code or configuration files.
This is not a recommended approach and is used only for brevity.

In production scenarios it is highly recommended to use more secure approaches to store sensitive configuration, such as:

Regardless of the configuration approach used for Mailtrap API client setup, all of them are using unified configuration model MailtrapClientOptions under the hood.

In the common case, the only required configuration setting is ApiToken.
Default is empty string, which will cause configuration validation to fail if left untouched.

There is a dedicated constructor, that takes string containing API token as a single parameter:

using Mailtrap.Configuration;

...

var config = new MailtrapClientOptions("<API_TOKEN>");

Other parameters receive defaults:

  • JSON minification is disabled
  • Default method for sending emails is set to transactional

But in case of need they can be changed to custom values during object construction or using configuration binding.

var config = new MailtrapClientOptions("<API_TOKEN>")
{
    PrettyJson = true,
    UseBulkApi = true
};

Authentication

ApiToken is used to set a token to authorize client requests to Mailtrap API.
This is the only required setting, that needs to be set explicitly, since by default it is set to empty string, which isn't a valid value and will throw an exception if left unchanged.

var config = new MailtrapClientOptions("<API_TOKEN>");

Default Send API

A combination of UseBulkApi and InboxId parameters is used to define default API for sending emails.

By default, if these parameters are not specified, transactional API will be used:

var config = new MailtrapClientOptions("<API_TOKEN>");

...

// Will send email as transactional.
var response = await _mailtrapClient.Email().Send(request);

If InboxId contains valid Inbox ID, default send API will be set to test, and all emails sent through Email() will be routed to the specified test inbox.

var config = new MailtrapClientOptions("<API_TOKEN>")
{
    InboxId = 1234,
    UseBulkApi = true // Will be ignored, since InboxId is set
};

...

// Will send email to the test inbox with ID '1234', specified by configuration.
var response = await _mailtrapClient.Email().Send(request);

In case Inbox is not specified (set to empty string or null), default send API will be set to transactional or bulk, depending on the UseBulkApi flag.

var config = new MailtrapClientOptions("<API_TOKEN>")
{
    UseBulkApi = true
};

...
// Will send email as bulk.
var response = await _mailtrapClient.Email().Send(request);

Serialization

PrettyJson flag can be opted-in to produce pretty JSON for outgoing HTTP requests, which can be helpful for debugging and log analysis.

By default it is set to false, and JSON that is sent in HTTP requests is minified.

var config = new MailtrapClientOptions("<API_TOKEN>");
{"to":[{"email":"john_doe@example.com","name":"John Doe"}],"from":{"email":"sales@example.com","name":"Example Sales Team"},...}

But when opted-in, outgoing HTTP request body will contain human-friendly JSON.

var config = new MailtrapClientOptions("<API_TOKEN>")
{
    PrettyJson = true
};
{
  "to": [
    {
      "email": "john_doe@example.com",
      "name": "John Doe"
    }
  ],
  "from": {
    "email": "sales@example.com",
    "name": "Example Sales Team"
  },
  ...
}

What's next

If you are using DI container in your application, please visit this article for detailed instructions how to configure and inject Mailtrap API client.

Alternatively, you can use standalone factory for configuring and spawning Mailtrap API client instances.