Appearance
Delivery feedback
A sent e-mail is only accepted: the relay or the API took it. Delivery feedback tells a product what happened after that, for each recipient. It records reports from the provider and hands every change to the product. The product decides how to react. Decision record: ADR 0017.
Outcomes
| Outcome | Meaning | Final |
|---|---|---|
Delivered | The recipient's mail server accepted the message | No: a later permanent bounce still wins |
TransientBounce | Temporary refusal: mailbox full, greylisting, a 4.x.x status | No |
PermanentBounce | Definitive refusal: unknown mailbox or domain, a 5.x.x status, a provider drop | Yes |
A recipient with no report is only accepted. The absence of a bounce is not proof of delivery. A bounce can arrive days later, and some servers drop mail without saying so.
The precedence rule is EmailDeliveryOutcomePrecedence.Supersedes:
- a permanent bounce replaces a delivery and is never replaced;
- a delivery replaces a transient bounce;
- a transient bounce replaces nothing it follows.
A report that changes nothing is still recorded, but it is not published. A notification moves from Sent to Delivered once every recipient is Delivered. A bounce never re-enqueues a notification or uses up a retry.
What each provider reports
| Source | Reports | How |
|---|---|---|
| SMTP (MailKit) | Refusal during the conversation only | A 5xx on RCPT TO is a permanent bounce, failure code EMAIL_RECIPIENT_REJECTED, reason Bounced, no retry. A 4xx fails with EMAIL_RECIPIENT_DEFERRED and is retried. |
| SendGrid | Delivered, bounce, blocked, deferred, dropped | The signed Event Webhook, mapped with MapGrydSendGridEventWebhook |
| Your own capture | Anything you can correlate | Call IEmailDeliveryFeedback.RecordAsync |
Behind an SMTP relay, almost every bounce arrives later as a DSN, which the module does not read. A control that must know an e-mail arrived needs a provider with feedback.
Correlation
Every e-mail carries X-Gryd-Notification-Id. The SMTP sender sets the MIME Message-ID to the notification id at the sender's domain, so it is the same on every retry. SendGrid receives the id as the custom argument gryd_notification_id and returns its X-Message-Id. The notification stores the provider's id in ProviderMessageId.
A report finds its notification by the echoed id, or else by ProviderMessageId. The report never supplies the tenant: the recorder takes it from the notification it finds.
Recording a report
IEmailDeliveryFeedback is registered when PersistNotifications=true.
csharp
var result = await feedback.RecordAsync(new EmailDeliveryReport
{
ProviderMessageId = providerMessageId, // or NotificationId
Address = "old-finance@supplier.example",
Outcome = EmailDeliveryOutcome.PermanentBounce,
Provider = "my-mailer", // [a-z0-9-], up to 32
ProviderEventId = providerEventId, // unique per provider
OccurredAt = occurredAt,
DiagnosticCode = "5.1.1"
}, ct);The result is one of these:
Changed: recorded, and published;Unchanged: recorded, but the outcome stays;Duplicate: the sameProviderandProviderEventIdwere already recorded;Unmatched: no e-mail notification, or no such recipient, was found.
A malformed report throws ArgumentException.
Reacting to a change
Register one or more IEmailDeliveryFeedbackHandler in the host:
csharp
builder.Services.AddScoped<IEmailDeliveryFeedbackHandler, BankChangeNoticeFeedback>();
internal sealed class BankChangeNoticeFeedback(IBankChangeProposals proposals) : IEmailDeliveryFeedbackHandler
{
public Task HandleAsync(EmailDeliveryFeedbackNotice notice, CancellationToken ct) => notice.Outcome switch
{
EmailDeliveryOutcome.Delivered => proposals.StartGracePeriodAsync(notice.OperationId, notice.FeedbackId, ct),
EmailDeliveryOutcome.PermanentBounce => proposals.TryNextContactAsync(notice.OperationId, notice.FeedbackId, ct),
_ => Task.CompletedTask
};
}The notice carries:
FeedbackId,NotificationId,OperationId,CorrelationIdandTenantId;- the address and the outcome;
- the diagnostic, the provider,
OccurredAtandRecordedAt.
The change is published in the same transaction that records it. The durable delivery worker hands it over afterwards, under the notification's tenant and with no user:
- Delivered at least once. Be idempotent by
FeedbackId. - Notices may arrive out of order. Treat a permanent bounce as final.
- A handler that throws gets the notice again, with a growing delay, up to 10 attempts. After that it is dead-lettered.
- Without
DurableDelivery=true, reports are recorded but not published. - A host with no handler completes the notices without doing anything.
A control also needs its own timeout for "no signal", because some servers never answer. That timeout is product policy.
SendGrid Event Webhook
Turn on Signed Event Webhook in SendGrid, select the delivery events (delivered, bounce, deferred and dropped) and copy the verification key:
csharp
builder.Services.AddGrydNotificationsSendGrid(options =>
{
options.ApiKey = configuration["SendGrid:ApiKey"]!;
options.EventWebhookPublicKey = configuration["SendGrid:EventWebhookPublicKey"];
});
app.MapGrydSendGridEventWebhook() // POST /webhooks/sendgrid/events
.RequireRateLimiting("webhooks");The endpoint allows anonymous requests, and the signature is its only authentication. It refuses:
- a missing or invalid signature, with
401; - a timestamp more than
EventWebhookTimestampToleranceaway (5 minutes by default), with401; - a body over
EventWebhookMaxBodyBytes(1 MiB by default), with413; - a signed body that is not an event array, with
400.
A verified batch answers 200, even when some events match nothing. A failure to record answers 500. SendGrid then resends, and each event is still recorded once by its sg_event_id. The body is not stored.
Event mapping:
| SendGrid event | Outcome |
|---|---|
delivered | Delivered |
bounce | PermanentBounce (TransientBounce when its type is blocked) |
blocked, deferred | TransientBounce |
dropped | PermanentBounce |
| anything else | ignored |
Privacy
Addresses are stored with each report and travel in the notice. Logs carry the provider and counts only, not addresses or provider text. Reports are deleted with their notification.