Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use IEmailSender?

I'm using AspNetCore.Identity at my project, so I write a EmailSender class that implements this interface.

Everything is working fine.

But this description about IEmailSender took my attention:

Microsoft.AspNetCore.Identity.UI.Services.IEmailSender
This API supports the ASP.NET Core Identity default UI infraestructure and is not intended to be used directly from your code. This API may change or be removed in future releases

enter image description here

If it isn't intended to be used directly in my code, what should I use? My application also send emails in other parts, not only for Identity.

like image 438
Guilherme de Jesus Santos Avatar asked May 06 '20 13:05

Guilherme de Jesus Santos


2 Answers

I don't think you need to use IEmailSender. In fact, based on my experience, to some degrees I think you shouldn't use it:

  1. In .NET 5.0, IEmailSender is in the package ASP.NET Core Identity.UI, with the default implementation injected in DI that doesn't do anything. It's perfectly fine if you want to use the default UI provided by the framework. But if you don't like to use those default pages and just want to use the core of the Identity framework, including the whole package just to get IEmailSender interface seems overkill to me. Not to mention there is an annoying bug when using Identity UI at this moment.

  2. It doesn't add any value. I would have been persuaded if the framework could automatically scan my concrete implementation and use that instead to send out emails. But that's not the case. You would have to inject your email sender through DI by services.AddSingleton<>() yourself and use it via constructor injection where you want to send emails.

  3. IEmailSender.SendEmailAsync(string email, string subject, string htmlMessage) method is pretty basic. Most of the time you even would have to create your own razor library to generate the HTML message yourself. In my case (using SendGrid v9.22 with dynamic template), I would need to not only pass the HTML message, but also the template data (for setting the placeholder in my SendGrid template via msg.SetTemplateData()). The basic SendEmailAsync() just doesn't have extra parameters for me to send additional data when sending the email.

Conclusion:

If you see yourself that you won't switch to email provider for a project, you might as well just create a concrete email sender that fits you well, and inject that into DI directly.

If you see there might be a time when your project will switch email provider, having a basic and simple interface, like IEmailSender, makes sense.

like image 65
David Liang Avatar answered Sep 28 '22 01:09

David Liang


You definitely should, as the IEmailSender is the best available option offered by the framework to send email messages, especially if you have to deal with ASP.NET Identity stuff (account confirmation, password recovery and so on).

However, in order to use it properly, you should implement it using one of the many third-party services (and their corresponding .NET NuGet packages), as suggested in this official post.

If you need further instruction to setup these email sender provider, you can take a look at these implementation guides that I wrote on my blog (DISCLAIMER: I'm not affiliated with any of them):

  • .NET Core General Purpose SMTP implementation using MailKit
  • .NET Core IEmailSender implementation using Twilio SendGrid API
  • .NET Core IEmailSender implementation using SendInBlue API

The fact that the API could get removed in the future shouldn't bother you at all, since all of these third-party packages have their own API: you'll be able to use them even without the IEmailSender interface (as a matter of fact, you already can).

like image 29
Darkseal Avatar answered Sep 28 '22 01:09

Darkseal