-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInbox.cs
More file actions
91 lines (69 loc) · 2.74 KB
/
Inbox.cs
File metadata and controls
91 lines (69 loc) · 2.74 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
using Mailtrap;
using Mailtrap.Accounts;
using Mailtrap.Inboxes;
using Mailtrap.Inboxes.Models;
using Mailtrap.Inboxes.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>>();
IMailtrapClient mailtrapClient = host.Services.GetRequiredService<IMailtrapClient>();
try
{
var accountId = 12345;
var projectId = 54321;
IAccountResource accountResource = mailtrapClient.Account(accountId);
var inboxName = "My Test Inbox";
// Get resource for inbox collection
IInboxCollectionResource inboxesResource = accountResource.Inboxes();
// Get all inboxes for account
IList<Inbox> inboxes = await inboxesResource.GetAll();
Inbox? inbox = inboxes
.FirstOrDefault(i => string.Equals(i.Name, inboxName, StringComparison.OrdinalIgnoreCase));
if (inbox is null)
{
logger.LogWarning("No inbox found. Creating.");
// Create inbox
var createInboxRequest = new CreateInboxRequest(projectId, inboxName);
inbox = await inboxesResource.Create(createInboxRequest);
}
else
{
logger.LogInformation("Inbox found.");
}
// Get resource for specific inbox
IInboxResource inboxResource = accountResource.Inbox(inbox.Id);
// Get Details
inbox = await inboxResource.GetDetails();
logger.LogInformation("Inbox: {Inbox}", inbox);
// Mark all messages in the inbox as read
Inbox updatedInbox = await inboxResource.MarkAsRead();
// Update inbox details
var updateInboxRequest = new UpdateInboxRequest
{
Name = "Updated Inbox Name"
};
updatedInbox = await inboxResource.Update(updateInboxRequest);
// Toggle email for inbox
// updatedInbox = await inboxResource.ToggleEmailAddress();
// Reset email address for inbox
updatedInbox = await inboxResource.ResetEmailAddress();
// Reset credentials for inbox
updatedInbox = await inboxResource.ResetCredentials();
logger.LogInformation("Updated Inbox: {Inbox}", updatedInbox);
// Delete inbox
// Beware that resource becomes invalid after deletion and should not be used anymore
Inbox deletedInbox = await inboxResource.Delete();
logger.LogInformation("Deleted Inbox: {Inbox}", deletedInbox);
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred during API call.");
Environment.FailFast(ex.Message);
throw;
}