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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
1

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; } } } 

2 Comments

I downvoted because I misinterpreted the answer. I realized my mistake but can't remove the downvote. RIP
@AlbatrossCafe - No problem :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.