[英]How to generate Html file with Mjml framework in ASP.NET Core MVC?
I am sending an e-mail using mailkit and mimekit like this:我正在使用 mailkit 和 mimekit 发送电子邮件,如下所示:
MimeMessage message = new MimeMessage();
message.From.Add(new MailboxAddress("najmahan", "santosh.metikoti@gmail.com"));
message.To.Add(MailboxAddress.Parse("najmahan.com"));
message.Subject = "Transactions between " + startDate + "and " + endDate;
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = $"<table><tr><th>Account</th><th>Amount</th><th>Date</th><th>Type</th></tr>";
foreach (var item in model.AllTransactions)
{
/*Console.WriteLine(item.Account_Name + "-" + item.Amount + "-" + item.Date);*/
bodyBuilder.HtmlBody += $"<tr><td>{item.Account_Name}</td><td>{item.Amount}</td><td>{item.Date}</td><td>{item.Type}</td></tr>";
}
foreach (var item in model.IncomeLists)
{
bodyBuilder.HtmlBody += $"<b>Total Income: {item.Income}</b><br>";
}
foreach (var item in model.ExpenseLists)
{
bodyBuilder.HtmlBody += $"<b>Total Expense: {item.Expense}</b><br>";
}
bodyBuilder.TextBody = "This is some plain text";
var prehtml = "<mjml><mj-body><mj-container><mj-section><mj-column><mj-text>Hello World!</mj-text></mj-column></mj-section></mj-container></mj-body></mjml>";
message.Body = bodyBuilder.ToMessageBody();
In this by using bodyBuilder.HtmlBody
.在此通过使用bodyBuilder.HtmlBody
。 I wanted to use MJML(responsive email framework) which should generate HTML text and append with message
.我想使用 MJML(响应式电子邮件框架),它应该生成 HTML 文本并附加message
。
How can I do this.我怎样才能做到这一点。 if there is just a default mjml code generated and append with message
will also help如果只生成默认的 mjml 代码并附加message
也会有所帮助
Please help me with this请在这件事上给予我帮助
I ran into a similar problem.我遇到了类似的问题。 To solve it, I found two optimal options:为了解决这个问题,我找到了两个最佳选择:
unofficial port of MJML 4 (by MailJet) . MJML 4 的非官方端口(由 MailJet 提供) 。 Made entirely on .Net.完全在 .Net 上制作。 Does not support two components ( mj-include and mj-html-attributes )不支持两个组件( mj-include和mj-html-attributes )
mjml-aspnetcore - uses nodejs. mjml-aspnetcore - 使用 nodejs。 Full support for the mjml standard.完全支持 mjml 标准。 Should be slower than the first version, requires installation of nodejs and mjml应该比第一个版本慢,需要安装nodejs和mjml
For first option solution will look like对于第一个选项解决方案将如下所示
var mjmlRenderer = new MjmlRenderer();
message.Body = mjmlRenderer.Render(prehtml, new MjmlOptions {
Beautify = false
}).Html;
ps.附言。 I apologize that the solution is not complete - copying code examples from github is a bad in my opinion.我很抱歉解决方案不完整 - 我认为从 github 复制代码示例是不好的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.