-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
131 lines (113 loc) · 4.49 KB
/
Copy pathProgram.cs
File metadata and controls
131 lines (113 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http.Headers;
using Mailtrap;
using Mailtrap.Configuration;
using Mailtrap.Emails.Requests;
using Mailtrap.Emails.Responses;
using Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Example demonstrating how to use standalone Mailtrap API client factory to spawn client instances.
/// </summary>
[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "Example")]
internal sealed class Program
{
private static async Task Main()
{
try
{
// Factory implements IDisposable and should be disposed properly after use.
using MailtrapClientFactory mailtrapClientFactory = CreateFactoryWithToken();
IMailtrapClient mailtrapClient = mailtrapClientFactory.CreateClient();
SendEmailRequest request = SendEmailRequest
.Create()
.From("john.doe@demomailtrap.com", "John Doe")
.To("hero.bill@galaxy.net")
.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() // Default client, depends on configuration
.Send(request);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred while sending email: {0}", ex);
Environment.FailFast(ex.Message);
throw;
}
}
/// <summary>
/// The simplest case, which uses API token to configure Mailtrap API client factory.
/// </summary>
private static MailtrapClientFactory CreateFactoryWithToken()
{
// For sure you should not hardcode apiKey for security reasons, and use environment variables
// or configuration files instead.
var apiToken = "<API-KEY>";
return new MailtrapClientFactory(apiToken);
}
/// <summary>
/// Other simple one, which uses configuration object to configure Mailtrap API client factory.
/// </summary>
private static MailtrapClientFactory CreateFactoryWithOptions()
{
var config = new MailtrapClientOptions("<API-KEY>")
{
InboxId = 12345
};
return new MailtrapClientFactory(config);
}
/// <summary>
/// Advanced scenario, where custom standalone HttpClient is used.
/// </summary>
private static MailtrapClientFactory CreateFactoryWithStandaloneHttpClient()
{
#pragma warning disable CA2000 // Dispose objects before losing scope
// Please ensure you manage HttpClient and HttpMeassageHandler lifecycle properly
// in the real-world application
var httpMessageHandler = new SocketsHttpHandler()
{
PooledConnectionLifetime = TimeSpan.FromMinutes(1)
};
var httpClient = new HttpClient(httpMessageHandler);
#pragma warning restore CA2000 // Dispose objects before losing scope
return new MailtrapClientFactory("<API-TOKEN>", httpClient);
}
/// <summary>
/// More advanced scenario, where HttpClient is configured with custom fine-grain settings delegate.
/// </summary>
private static MailtrapClientFactory CreateFactoryWithAdvancedHttpClientConfiguration()
{
var config = new MailtrapClientOptions
{
ApiToken = "<API-KEY>", // Required
UseBulkApi = true
};
return new MailtrapClientFactory(config, httpClientBuilder =>
{
// Adding resilience handler
httpClientBuilder.AddStandardResilienceHandler();
// Adding hedging handler
httpClientBuilder.AddStandardHedgingHandler();
// Configuring HttpClient
httpClientBuilder.ConfigureHttpClient(client =>
{
client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
{
NoCache = true
};
});
// Adding proxy
httpClientBuilder.ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler()
{
Proxy = new WebProxy("proxy.mailtrap.io", 8080),
CheckCertificateRevocationList = true
};
});
// Configure detailed HTTP request logging
httpClientBuilder.AddExtendedHttpClientLogging();
});
}
}