-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
61 lines (49 loc) · 2.06 KB
/
Copy pathProgram.cs
File metadata and controls
61 lines (49 loc) · 2.06 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
using Mailtrap;
using Mailtrap.Organizations;
using Mailtrap.Organizations.Models;
using Mailtrap.Organizations.Requests;
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>>();
// Sub accounts live under /api/organizations/{organization_id}/... so they are
// accessed via the organization-scoped client, not IMailtrapClient.
IMailtrapOrganizationClient organizationClient = host.Services.GetRequiredService<IMailtrapOrganizationClient>();
try
{
var organizationId = 12345;
IOrganizationResource organizationResource = organizationClient.Organization(organizationId);
// Get resource for sub accounts collection
IOrganizationSubAccountCollectionResource subAccountsResource = organizationResource.SubAccounts();
// List sub accounts of the organization
IList<SubAccount> subAccounts = await subAccountsResource.GetAll();
logger.LogInformation("Found {Count} sub account(s).", subAccounts.Count);
foreach (SubAccount subAccount in subAccounts)
{
logger.LogInformation("Sub Account: Id={Id}, Name={Name}", subAccount.Id, subAccount.Name);
}
// Create a new sub account under the organization
var createRequest = new CreateSubAccountRequest
{
Account = new SubAccountAttributes
{
Name = "Demo Sub Account"
}
};
SubAccount createdSubAccount = await subAccountsResource.Create(createRequest);
logger.LogInformation(
"Created Sub Account: Id={Id}, Name={Name}",
createdSubAccount.Id,
createdSubAccount.Name);
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred during API call.");
Environment.ExitCode = 1;
return;
}