Search code examples
razorblazormailkitmimekitblazor-client-side

Is it possible to get raw HTML from Razor/Blazor components?


I'd like to set up a "mailer/newsletter" using MailKit. My site stack is based off of Blazor web assembly and uses .Razor components.

I'm wondering if there is a way to consume a razor component I've written to output HTML into the MimeMessage object I'm using to generate my email body and what that architecture would look like / the best way to accomplish this?

Similar question (though not Blazor):


Solution

  • Late answer since I just saw this question: I wrote an alternative system called BlazorTemplater which uses .razor files instead of .cshtml since I had exactly this problem.

    You can convert your templates to .razor format and then use BlazorTemplater to render to HTML:

    var html = new ComponentRenderer<MyRazorClass>()
       .Set(c => c.SomeParameter = someValue)
       .Render();
    

    It supports parameters, DI injection and nested components so you should find it useful! It's also much easier to set up and works in Razor Class Libraries too.