Skip to main content

Curiosus.Notifications.EMail

NuGet Downloads Coverage

EMail channel for Curiosus.Notifications. It sends EmailNotifications through the IEMailSender registered in the container (SMTP, Mailgun, UnisenderGo, ...).

Installation​

dotnet add package Curiosus.Notifications.EMail

Usage​

Register an EMail sender, the channel and a builder that turns your metadata into emails:

services.AddSmtpEMailSender(smtpOptions); // any IEMailSender implementation
services.AddCuriosusEMailChannel(); // also registers INotificator
services.AddCuriosusNotificationBuilder<UserRegisteredEmailBuilder>();
public class UserRegistered : INotificationMetadata
{
public string Email { get; init; } = null!;
public string Name { get; init; } = null!;
}

public class UserRegisteredEmailBuilder : EmailNotificationBuilderBase<UserRegistered>
{
protected override Task<IReadOnlyList<INotification>> BuildNotificationsAsync(
UserRegistered metadata,
CancellationToken cancellationToken = default)
{
var email = new EmailNotification(
metadata.Email,
"Welcome!",
$"<p>Hello, {metadata.Name}!</p>",
isBodyHtml: true);

return Task.FromResult<IReadOnlyList<INotification>>(new[] { email });
}
}

await notificator.NotifyAsync(new UserRegistered { Email = "user@example.com", Name = "John" }, cancellationToken);

The channel type is EmailNotification.Type (curiosus.notifications.email). Sender errors are thrown as NotificationException with a mapped NotificationErrorCode (Auth, Communication, RateLimit, NoMoney, IncorrectRequestData, Unknown). Provider-specific options can be passed via IEMailExtraParams.

To act on every sending result (logging, analytics), implement IEMailNotificationPostProcessor and register it with services.AddEMailNotificationPostProcessor<MyPostProcessor>().

See also​