如何在asp.net中显示html电子邮件

时间:2013-11-06 20:56:08

标签: c# asp.net-mvc razor html-email

我试图从System.Net.Mail.AlternatView中删除剃刀视图中的html电子邮件。我似乎无法在互联网上找到任何说明如何做到这一点。 everytihng与创建电子邮件有关,而不是显示电子邮件。

任何人都可以帮忙吗?

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

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

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

感谢

1 个答案:

答案 0 :(得分:1)

基于此Retrieving AlternateView's of email答案,您可以使用函数将流作为字符串

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