Client Configuration - Standalone Factory
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:
While DI pattern is recommended approach to configure and use Mailtrap API client, alternatively, MailtrapClientFactory can be used to spawn client instances, when usage of DI container isn't possible or desired.
In most cases factory configuration is pretty similar to the DI container one.
Important
Please consider that MailtrapClientFactory implements IDisposable interface and must be disposed properly after use.
Basic usage
In the simplest scenario getting client is as simple as the following:
using Mailtrap;
...
// Creating factory instance
using var factory = new MailtrapClientFactory("<API_TOKEN>");
// Spawning client
IMailtrapClient client = factory.CreateClient();
Configuration with MailtrapClientOptions instance
Alternatively, the same way as with DI container, factory can be configured using pre-created instance of MailtrapClientOptions with additional parameters set to non-default values:
using Mailtrap;
using Mailtrap.Configuration;
...
// Creating new instance of `MailtrapClientOptions`
var config = new MailtrapClientOptions("<API_TOKEN>")
{
InboxId = 12345
};
// Creating factory instance
using var factory = new MailtrapClientFactory(config);
// Spawning Mailtrap API client
IMailtrapClient client = factory.CreateClient();
Advanced HttpClient configuration
Optionally, a delegate for advanced HTTP pipeline configuration, which takes IHttpClientBuilder as a parameter, can be passed as a second parameter to factory constructor:
using Mailtrap;
...
// Creating factory instance
using var factory = new MailtrapClientFactory("<API_TOKEN>", (IHttpClientBuilder builder) =>
{
// Do any required HttpClient configuration
// builder.AddStandardResilienceHandler();
// builder.AddExtendedHttpClientLogging();
// etc.
});
// Spawning Mailtrap API client
IMailtrapClient client = factory.CreateClient();
As an alternative to the aforementioned scenario, external pre-configured instance of HttpClient can be passed instead of delegate.
using System.Net.Http;
using Mailtrap;
...
// Assuming there is some already existing instance.
using var httpClient = new HttpClient();
// Creating factory instance
using var factory = new MailtrapClientFactory("<API_TOKEN>", httpClient);
// Spawning Mailtrap API client
IMailtrapClient client = factory.CreateClient();
Important
Please consider that in the latter scenario the caller is responsible of the HttpClient lifetime control and proper disposal.
What's next
Additional examples of the Mailtrap API client factory configuration and usage can be found on GitHub
Various client usage examples and recipes can be found in Cookbook.
Also a bunch of examples is available in the Source Repository.
Detailed API reference is available here.