4

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):

Taylor C. White
  • 836
  • 1
  • 12
  • 30

2 Answers2

2

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.

Quango
  • 12,338
  • 6
  • 48
  • 83
  • 1
    Gosh, I asked this question 2 years ago now...so crazy. Thanks so much for this. Marking as correct answer. I'm in the process of installing your package and consuming it now. Will report back! – Taylor C. White Mar 17 '22 at 15:29
0

I am using Blazor with MailKit here: Google Email Viewer in Server Side Blazor

I use MarkupString to display the email content like this:

@using MimeKit
@using MessageReader
@strError
<div style="padding:2px; vertical-align:top">
    <div><i>@MimeKitMessage.Date.ToString()</i></div>
    <div><b>From:</b> @MimeKitMessage.From.ToString()</div>
    <div><b>To:</b> @MimeKitMessage.To.ToString()</div>
    <div><b>Subject:</b> @MimeKitMessage.Subject</div>
    <br />
    <div>@((MarkupString)@htmlEmail)</div>
</div>
@code {
    [Parameter] public Message paramMessage { get; set; }
    MimeMessage MimeKitMessage;
    string strError = "";
    string htmlEmail = "";
    protected override void OnInitialized()
    {
        try
        {
            if (paramMessage != null)
            {
                string converted = paramMessage.Raw.Replace('-', '+');
                converted = converted.Replace('_', '/');
                byte[] decodedByte = Convert.FromBase64String(converted);
                using (Stream stream = new MemoryStream(decodedByte))
                {
                    // Convert to MimeKit from GMail
                    // Load a MimeMessage from a stream
                    MimeKitMessage = MimeMessage.Load(stream);
                    // Convert any embedded images
                    var visitor = new HtmlPreviewVisitor();
                    MimeKitMessage.Accept(visitor);
                    htmlEmail = visitor.HtmlBody;
                    //If the email has attachments we can get them here
                    //var attachments = visitor.Attachments;
                }
            }
        }
        catch (Exception ex)
        {
            strError = ex.Message;
        }
    }    
}
Michael Washington
  • 2,744
  • 20
  • 29