-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
97 lines (79 loc) · 2.34 KB
/
Copy pathProgram.cs
File metadata and controls
97 lines (79 loc) · 2.34 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
using SquidStd.Core.Interfaces.Events;
using SquidStd.Mail.Abstractions.Data;
using SquidStd.Mail.Abstractions.Data.Events;
using SquidStd.Mail.Abstractions.Interfaces;
using SquidStd.Mail.Abstractions.Types.Mail;
using SquidStd.Mail.MailKit.Extensions;
using SquidStd.Mail.Queue.Extensions;
using SquidStd.Mail.Queue.Interfaces;
using SquidStd.Messaging.Extensions;
using SquidStd.Services.Core.Services.Bootstrap;
var bootstrap = SquidStdBootstrap.Create(
new()
{
ConfigName = "squidstd",
RootDirectory = AppContext.BaseDirectory
}
);
#region step-1
bootstrap.ConfigureServices(
container => container.AddMail(
new()
{
Protocol = MailProtocolType.Imap,
Host = "imap.example.com",
Port = 993,
Username = "alice@example.com",
Password = "app-password"
}
)
);
#endregion
#region step-2
bootstrap.ConfigureServices(
container => container.AddMailSender(
new()
{
Host = "smtp.example.com",
Port = 587
}
)
);
#endregion
#region step-3
bootstrap.ConfigureServices(
container => container
.AddInMemoryMessaging()
.AddMailQueue()
);
#endregion
await bootstrap.StartAsync();
// Inbound: react to each received email on the event bus.
var eventBus = bootstrap.Resolve<IEventBus>();
eventBus.RegisterListener(new MailReceivedLogger());
var outgoing = new OutgoingMailMessage
{
To = [new("Bob", "bob@example.com")],
Subject = "Hi",
HtmlBody = "<p>Hi</p>"
};
// Outbound: queue for background sending (no network call).
var queue = bootstrap.Resolve<IMailQueue>();
await queue.EnqueueAsync(outgoing);
// Or send synchronously; guarded so the sample runs without a live SMTP server.
if (args.Contains("--send"))
{
var sender = bootstrap.Resolve<IMailSender>();
await sender.SendAsync(outgoing);
}
await bootstrap.StopAsync();
/// <summary>Logs every received email as it arrives on the event bus.</summary>
public sealed class MailReceivedLogger : IEventListener<MailReceivedEvent>
{
/// <summary>Handles a received-mail event.</summary>
public Task HandleAsync(MailReceivedEvent eventData, CancellationToken cancellationToken)
{
Console.WriteLine($"received: {eventData.Message.Subject}");
return Task.CompletedTask;
}
}