使用模板.Net Core发送新闻稿

时间:2018-08-16 09:47:19

标签: c# asp.net-core html-email razorengine

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

enter image description here

我在此看到的很多问题只会在电子邮件中添加一项。例如herehere。我正在使用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();
            }

我能得到我想要的布局吗?

1 个答案:

答案 0 :(得分:1)

经过长时间的搜索,我在answer上找到了答案,这使我能够得到我一直在寻找的答案。首先,我将此代码添加到我的构造函数中

 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();

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