|
| 1 | +# Email: receive, send, and queue |
| 2 | + |
| 3 | +Poll a mailbox for inbound mail, send outbound mail over SMTP, and queue mail for background delivery. |
| 4 | + |
| 5 | +## What you'll build |
| 6 | + |
| 7 | +A host using `SquidStd.Mail.MailKit` and `SquidStd.Mail.Queue`: an IMAP poller that raises a `MailReceivedEvent` |
| 8 | +for each new email, an SMTP sender (`IMailSender`), and a fire-and-forget send queue (`IMailQueue`) backed by |
| 9 | +messaging. The contracts live in `SquidStd.Mail.Abstractions`. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- .NET 10 SDK |
| 14 | +- `dotnet add package SquidStd.Mail.MailKit` |
| 15 | +- `dotnet add package SquidStd.Mail.Queue` |
| 16 | +- Real IMAP/SMTP credentials are only needed to actually connect; the sample compiles and runs without them. |
| 17 | + |
| 18 | +## Steps |
| 19 | + |
| 20 | +### 1. Receive: poll a mailbox |
| 21 | + |
| 22 | +`AddMail` registers an IMAP/POP3 poller; each received message is published as a `MailReceivedEvent`, which an |
| 23 | +`IAsyncEventListener<MailReceivedEvent>` handles. |
| 24 | + |
| 25 | +[!code-csharp[](../../samples/SquidStd.Samples.Email/Program.cs#step-1)] |
| 26 | + |
| 27 | +### 2. Send: outbound SMTP |
| 28 | + |
| 29 | +`AddMailSender` registers `IMailSender`; build an `OutgoingMailMessage` and call `SendAsync`. |
| 30 | + |
| 31 | +[!code-csharp[](../../samples/SquidStd.Samples.Email/Program.cs#step-2)] |
| 32 | + |
| 33 | +### 3. Queue: background delivery |
| 34 | + |
| 35 | +`AddInMemoryMessaging` plus `AddMailQueue` register `IMailQueue`; `EnqueueAsync` hands the message to a |
| 36 | +background consumer that sends it via the SMTP sender. |
| 37 | + |
| 38 | +[!code-csharp[](../../samples/SquidStd.Samples.Email/Program.cs#step-3)] |
| 39 | + |
| 40 | +## Run it |
| 41 | + |
| 42 | +```bash |
| 43 | +dotnet run --project samples/SquidStd.Samples.Email |
| 44 | +``` |
| 45 | + |
| 46 | +It registers the poller, sender, and queue, registers the received-mail listener, and enqueues a message. |
| 47 | +Pass `--send` to also attempt a synchronous SMTP send (requires a reachable server). |
| 48 | + |
| 49 | +## How it works |
| 50 | + |
| 51 | +The poller runs on the timer wheel, fetches new messages over IMAP/POP3 with MailKit, and publishes a |
| 52 | +`MailReceivedEvent` on the event bus. `IMailSender` maps `OutgoingMailMessage` to a MIME message and sends it. |
| 53 | +`AddMailQueue` layers a queue over `SquidStd.Messaging`: `EnqueueAsync` publishes to the queue and a background |
| 54 | +consumer (`MailSendConsumerService`) drains it through `IMailSender`, decoupling request latency from delivery. |
| 55 | + |
| 56 | +## See also |
| 57 | + |
| 58 | +- [SquidStd.Mail.MailKit reference](../articles/mail-mailkit.md) |
| 59 | +- [SquidStd.Mail.Queue reference](../articles/mail-queue.md) |
| 60 | +- [SquidStd.Mail.Abstractions reference](../articles/mail-abstractions.md) |
0 commit comments