-
Notifications
You must be signed in to change notification settings - Fork 11
Emails
Emails send handling. It is built around standard class MailMessage. In a simple cases, only IEmailSender interface will be used with only one method.
Email sender interface. Example of registration with Autofac:
// Emails.
var emailSender = new Saritasa.Tools.Emails.SmtpClientEmailSender();
builder.RegisterInstance(emailSender).AsImplementedInterfaces().SingleInstance();Task SendAsync(MailMessage message, CancellationToken cancellationToken);Sends the specified message.
Right now only SmtpClientEmailSender is available that uses SmtpClient to send emails. In the future we will probably add providers for SendGrid and AWS.
Utilizes SmtpClient to send emails. Unlike standard SmtpClient you can call SendAsync method without any lock. It puts messages into the queue before sending it. Thread-safe.
- MaxQueueSize. Max queue size. If queue size for some reason is exceeded the EmailQueueExceededException exception will be thrown. The default value is 10240.
Handles several SMTP clients to send emails using a round-robin method. The main parameter is smtpClientInstancesCount that specifies how many SMTP clients to use (default is 2) to send emails simultaneously.
You can set additional behavior before and after emails sending by using interceptors. Interceptors are handlers that are executed before or after email sending. They can even cancel it. You can use them with EmailSender class. Here is a simple example of how it can be used to filter users out by email address (what sometimes needed for a development environment):
var emailSender = new SmtpClientEmailSender();
var filterInterceptor = new ApprovedEmailPatternsInterceptor("*@saritasa.com; *@saritasa-hosting.com");
emailSender.AddInterceptor(filterInterceptor);Now only emails to saritasa.com and saritasa-hosting.com domains will be accepted. The following interceptors are available:
Filters users to whom send an email. You can * and ? special characters to apply the mask. The symbols ,, ; and space can be used as separators. For example *@yandex.ru;*+test@saritasa.com.
Filters users to whom emails cannot be sent. You can * and ? special characters to apply the mask. The symbols ,, ; and space can be used as separators.
Save email to disk as .eml file before or after sending.
Contains counters of sending and sent emails. Thread-safe.
ThreadPool is not a safe place to keep email processing. See the article QueueBackgroundWorkItem to reliably schedule and run background processes in ASP.NET. You should implement your own decorator class to do that:
/// <summary>
/// The email sender decorates <see cref="IEmailSender" /> class. It adds messages to queue
/// using <see cref="HostingEnvironment.QueueBackgroundWorkItem(Action{System.Threading.CancellationToken})" />
/// method.
/// </summary>
public class AspNetSmtpEmailSender : IEmailSender
{
private readonly IEmailSender actualSender;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="actualSender">Actual sender need to decorate.</param>
public AspNetSmtpEmailSender(IEmailSender actualSender)
{
Guard.IsNotNull(actualSender, nameof(actualSender));
this.actualSender = actualSender;
}
/// <inheritdoc />
public Task SendAsync(MailMessage message)
{
HostingEnvironment.QueueBackgroundWorkItem(async ct =>
{
await actualSender.SendAsync(message).ConfigureAwait(false);
});
return Task.FromResult(true);
}
}