-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAttachment.cs
More file actions
64 lines (49 loc) · 1.85 KB
/
Copy pathAttachment.cs
File metadata and controls
64 lines (49 loc) · 1.85 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
using Mailtrap;
using Mailtrap.Attachments;
using Mailtrap.Attachments.Models;
using Mailtrap.Core.Models;
using Mailtrap.TestingMessages;
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 inboxId = 54321;
var messageId = 1122334455;
ITestingMessageResource messageResource = mailtrapClient
.Account(accountId)
.Inbox(inboxId)
.Message(messageId);
// Get resource for attachments collection
IAttachmentCollectionResource attachmentsResource = messageResource.Attachments();
var attachmentFilter = new EmailAttachmentFilter
{
Disposition = DispositionType.Attachment
};
IList<EmailAttachment> attachments = await attachmentsResource.Fetch(attachmentFilter);
EmailAttachment? attachment = attachments.FirstOrDefault();
if (attachment is null)
{
logger.LogWarning("No attachments found.");
return;
}
// Get resource for specific attachment
IAttachmentResource attachmentResource = messageResource.Attachment(attachment.Id);
// Get attachment details
attachment = await attachmentResource.GetDetails();
logger.LogInformation("Attachment: {Attachment}", attachment);
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred during API call.");
Environment.FailFast(ex.Message);
throw;
}