Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email Sender Library with Razor (.cshtml) Templates. .net core 2.0

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?

like image 281
akd Avatar asked Apr 05 '18 17:04

akd


1 Answers

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

like image 50
Gavril Ognjanovski Avatar answered Nov 15 '22 13:11

Gavril Ognjanovski