使用模板 .Net Core 发送时事通讯

Sending Newsletter using template .Net Core

我的任务是建立一种管理员用户可以轻松选择文章发送给订阅者列表的方式。下图是我要创建的,但我的问题是如何通过电子邮件发送视图。我将它创建为静态模板和剃刀模板。我尝试使用 RazorEngine and Razorlight 但无法弄清楚。

我在这方面看到的很多问题只在电子邮件中添加了一个项目。例如 here and here. I am using MailKit 发送电子邮件,但对于我的生活,我无法弄清楚如何获得上述电子邮件正文。我的代码看起来像这样

   var mimeMessage = new MimeMessage();
            mimeMessage.From.Add(new MailboxAddress("email", "intranet@domain.com"));
            foreach (var item in contacts)
            {
                mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, string.Join(",", item.Email.ToString())));
            }
            mimeMessage.Subject = Subject;
            var builder = new BodyBuilder();
            foreach (var item in post)
            {
                // Set the plain-text version of the message text
                builder.TextBody = item.Intro;
                 var ContentId = MimeUtils.GenerateMessageId();
                // Set the html version of the message text
                builder.HtmlBody = string.Format(item.Intro,@"<img src=""cid:{0}"">", item.FeaturedImage);
                // Now we just need to set the message body and we're done
                mimeMessage.Body = builder.ToMessageBody();
            }

我可以这样得到我想要的布局吗?

经过很长时间的搜索,我发现了这个 ,这使我能够得到我正在寻找的答案。首先,我将此代码添加到我的构造函数

 public class PostsController : Controller
{
        private ICompositeViewEngine _viewEngine;

       public PostsController(ICompositeViewEngine viewEngine)
       {
           _viewEngine = viewEngine;
       }

 } 

为了将视图呈现为字符串,我使用了这个函数

       private async Task<string> RenderViewToString(string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = ControllerContext.ActionDescriptor.ActionName;

        ViewData.Model = model;

        using (var writer = new StringWriter())
        {
            ViewEngineResult viewResult =
                _viewEngine.FindView(ControllerContext, viewName, false);

            ViewContext viewContext = new ViewContext(
                ControllerContext,
                viewResult.View,
                ViewData,
                TempData,
                writer,
                new HtmlHelperOptions()
            );

            await viewResult.View.RenderAsync(viewContext);

            return writer.GetStringBuilder().ToString();
        }
    }

我这样称呼它var renderedView = await RenderViewToString("NameOfView", Model);模型就是我要显示的数据。

var mimeMessage = new MimeMessage();
        mimeMessage.From.Add(new MailboxAddress("email", "intranet@domain.com"));
        foreach (var item in contacts)
        {
            mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, string.Join(",", item.Email.ToString())));
        }
        mimeMessage.Subject = Subject;
        var builder = new BodyBuilder();                                             
            // Set the html version of the message text
            builder.HtmlBody = renderedView;
            // Now we just need to set the message body and we're done
            mimeMessage.Body = builder.ToMessageBody();

这使我能够获得所需的视图。