7

I have been searching for this ages for a clear solution for sending emails with email templates from embedded source files and still I haven't found a clear answer to this.

Razor views are really nice to use as a template for emails.

I am trying to to create a class library as a package that will contain views(cshtml files) and models for each view. That class library will also have a service as EmailSender.

Then that class library will be referenced in a web application to send emails. The library obviously be re-usable in other projects.

EmailSender service will have methods for each email types. The web application will pass the model and class library will prepare the email content (parse the model into view) and return the string content. That string content will be sent as htmlbody email.

I have looked at this RazorLight library. This doesnt seem to be completed or updated for core 2.0 yet.

Another solution I came cross is here. The issue with this approach is that email templates do not contain model properties. Basically, templates contain {0}, {1} etc and format is used to parse the model data into template. If there are a lot of parameters in the template it would be hard to remember which parameter is which.

I just recently seen this article ASP.NET Core 2.1.0-preview1: Razor UI in class libraries

It tells that Core 2.1.0 will enable to add views in class libraries. Would this be the solution?

Or do you have any better idea to achieve this?

akd
  • 6,538
  • 16
  • 70
  • 112
  • I have a [ViewRenderer](https://github.com/cloudscribe/cloudscribe/blob/master/src/cloudscribe.Web.Common/Razor/ViewRenderer.cs) that I use to generate email using strongly typed models and razor views. It is available in a nuget for cloudscribe.Web.Common. You can see example usage in the [SiteEmailMessageSender](https://github.com/cloudscribe/cloudscribe/blob/master/src/cloudscribe.Core.Web/Components/Messaging/SiteEmailMessageSender.cs) in my cloudscribe core solution – Joe Audette Apr 05 '18 at 17:53
  • Also have [cloudscribe.Email.Senders](https://github.com/cloudscribe/cloudscribe/tree/master/src/cloudscribe.Email.Senders) which is available as a nuget and supports smtp, sendgrid, mailgun, and elastic email – Joe Audette Apr 05 '18 at 17:55
  • Thank you @JoeAudette for your answer. This looks promising but I have checked your example and I can see that you are referencing email templates as: var htmlMessage = await _viewRenderer.RenderViewAsString("EmailTemplates/ConfirmAccountHtmlEmail", model).ConfigureAwait(false); Where are those templates located? I should just create a folder as EmailTemplates at the root of my class library and add your libraries in my library and it should work? – akd Apr 05 '18 at 21:08
  • I keep all my views for cloudscribe Core including the email templates in a separate class library [built from this project](https://github.com/cloudscribe/cloudscribe/tree/master/src/cloudscribe.Core.Web.Views.Bootstrap3) currently they are used as embedded resources with extension method to add them in startup of the main app. In asp.net core 2.1 we will be able to pre-compile them in the class library. Views can be copied locally to the web app views and will override the ones from the embedded resources – Joe Audette Apr 05 '18 at 21:25
  • I am a bit confused now. How easily I can integrate this into my own class library? I have added cloudscribe.Web.Common and cloudscribe.Email.Sender library in my class library. I have created a folder in my class library as EmailTemplates and added cshtml email templates under. What else I should do to reference those templates in my code to prepare html emails for sending? – akd Apr 05 '18 at 21:34
  • we aren't supposed to chat in the comments but happy to help more in my gitter web chat here https://gitter.im/joeaudette/cloudscribe – Joe Audette Apr 07 '18 at 12:00
  • 1
    my solution works from class libraries but only in the context of a web app, RazorLight is supposed to work without MVC, but like you I could not get it working, I was going to use it from a windows service – Joe Audette Apr 10 '18 at 12:39
  • Thanks @Joe. Your library works great and simplifies generating emails. I would suggest you to write a blog about this library. Or better create a single nuget package that people could easily install in their project and get up and running with email templates. Thanks again. – akd Jan 30 '20 at 09:24

1 Answers1

1

I was also working on this type of problem and didn't find any solution on the web. So after some research I succeed to implement this kind of solution and wrote a blog. You can check the link below if it fits your solution.

P.S. Using Razor UI in class libraries may be a possible solution, however I haven't try it yet.

https://medium.com/@ognjanovski.gavril/net-core-email-sender-library-with-razor-templates-cshtml-contained-in-it-71c48bef1457