-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
69 lines (50 loc) · 2.08 KB
/
Program.cs
File metadata and controls
69 lines (50 loc) · 2.08 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
using Mailtrap;
using Mailtrap.Accounts;
using Mailtrap.Suppressions;
using Mailtrap.Suppressions.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
HostApplicationBuilder hostBuilder = Host.CreateApplicationBuilder(args);
IConfigurationSection config = hostBuilder.Configuration.GetSection("Mailtrap");
hostBuilder.Services.AddMailtrapClient(config);
using IHost host = hostBuilder.Build();
ILogger<Program> logger = host.Services.GetRequiredService<ILogger<Program>>();
IMailtrapClient mailtrapClient = host.Services.GetRequiredService<IMailtrapClient>();
try
{
var accountId = 12345;
IAccountResource accountResource = mailtrapClient.Account(accountId);
// Get resource for suppressions collection
ISuppressionCollectionResource suppressionsResource = accountResource.Suppressions();
var suppressionFilter = new SuppressionFilter
{
Email = "test@demomailtrap.co"
};
IList<Suppression> suppressions = await suppressionsResource.Fetch(suppressionFilter);
Suppression? suppression = suppressions.FirstOrDefault();
if (suppression is null)
{
logger.LogWarning("No suppressions found for the specified email {Email}.", suppressionFilter.Email);
logger.LogWarning("Retry without filter to get all suppressions.");
suppressions = await suppressionsResource.Fetch();
if (suppressions.Count == 0)
{
logger.LogWarning("No suppressions found.");
return;
}
suppression = suppressions.First();
}
// Get resource for specific suppression
ISuppressionResource suppressionResource = accountResource.Suppression(suppression.Id);
// Delete the suppression
Suppression? deletedSuppressionDetails = await suppressionResource.Delete();
logger.LogInformation("Deleted suppression: {Suppression}", deletedSuppressionDetails);
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred during API call.");
Environment.ExitCode = 1;
return;
}