2

我正在尝试从 System.Net.Mail.AlternatView 在剃刀视图中显示 html 电子邮件。我似乎无法在互联网上找到任何说明如何执行此操作的内容。一切都与创建电子邮件有关,而不是显示它们。

有人可以帮忙吗?

我一直在尝试这样的事情:

 @{var stream = Model.AlternateViews[0].ContentStream;
  using (StreamReader reader = new StreamReader(stream))
  {
      Html.Raw(reader.ReadToEnd());
      Html.Display(reader.ReadToEnd());
  }

我不确定如何提取 html 内容或如何显示它

谢谢

4

1 回答 1

1

基于这个Retrieving AlternateView's of email Answer,您可以只具有将流作为字符串获取的功能

   public string ExtractAlternateView()
    {
        var message = new System.Net.Mail.MailMessage();
        message.Body = "This is the TEXT version";

        //Add textBody as an AlternateView
        message.AlternateViews.Add(
            System.Net.Mail.AlternateView.CreateAlternateViewFromString(
                "This is the HTML version",
                new System.Net.Mime.ContentType("text/html")
            )
        );

        var dataStream = Model.AlternateViews[0].ContentStream;
        byte[] byteBuffer = new byte[dataStream.Length];
        return System.Text.Encoding.ASCII.GetString(byteBuffer, 0, dataStream.Read(byteBuffer, 0, byteBuffer.Length));
    }

接着

ViewBag.Message= string ExtractAlternateView();

 @Html.Raw(ViewBag.Message);
于 2013-11-06T21:25:29.840 回答