Search code examples
c#asp.net-coreasp.net-identity

IEmailSender outside of the UI project


When using Razor UI to handle registration, you need to implement IEmailSender and register it in startup.cs. Is there a way to use a mail class in another class library project within the solution? The mail class is something I would want kept out of the UI project.

I could probably install the nuget package for Microsoft.AspNetCore.Identity.UI in the class library, but that comes with an entourage of about 50 dependencies. Is there a better, more lightweight way?


Solution

  • I was just having a long evening and the answer wasn't coming to me. A good point was made in the comment about wrapping the external service so I have done exactly that.

    Here the EmailSender class just wraps my IMailService which is implemented in a class library project.

    public class EmailSender : IEmailSender
    {
        private readonly IMailService mailService;
    
        public EmailSender(IMailService mailService)
        {
            this.mailService = mailService ?? throw new ArgumentNullException(nameof(mailService));
        }
    
        public Task SendEmailAsync(string email, string subject, string htmlMessage)
        {
            return Task.Run(() => mailService.Send(new string[] { email }, subject, htmlMessage));
        }
    }