This library provides a simple way to send fault tolerant SMTP emails in .NET applications using OneOf
- Supports sending plain text and HTML emails.
- Supports sending emails to multiple recipients and adding CC and BCC recipients.
- Uses OneOf return types.
To install the library, run the following command in the Package Manager Console:
dotnet add package Biplov.Email.Smtp --version 0.1.3
To use the library first configure the SmtpCredentials in appsettings.json
then via services then invoke the AddSmtpEmailService
method passing the IAsncPolicy of Polly.
"SmtpCredentials": {
"Host": "<Your Smtp host>",
"Port": <Your smtp port>,
"Username": "<Your smtp username>",
"Password": "<Your smtp password>",
"UseSsl": <true or false>,
"Timeout": <timeout in milliseconds>
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<SmtpCredentials>(Configuration.GetSection("SmtpCredentials"));
var asyncPolicy = Policy
.TimeoutAsync(TimeSpan.FromSeconds(30))
.WrapAsync(Policy
.Handle<TimeoutException>()
.WaitAndRetryAsync(
retryCount: 3,
sleepDurationProvider: attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt))
));
services.AddSmtpEmailService(asyncPolicy);
}
var emailService = serviceProvider.GetRequiredService<IEmailService>();
var result = await emailService.SendAsync(
"Hello World",
"This is a test email from SmtpEmail",
new List<string> { "recipient@example.com" },
"sender@example.com"
);
result.Match(
success => Console.WriteLine("Email sent successfully."),
formatException => Console.WriteLine($"Email format exception: {formatException.Message}"),
authenticationException => Console.WriteLine($"Authentication failed: {authenticationException.Message}"),
smtpException => Console.WriteLine($"SMTP error: {smtpException.Message}"),
timeoutException => Console.WriteLine($"SMTP connection timed out: {timeoutException.Message}"),
exception => Console.WriteLine($"Unhandled exception: {exception.Message}")
);