简体   繁体   English

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

[英]how to display an html email in asp.net

i am trying to dislpay an html email in a razor view from System.Net.Mail.AlternatView. 我试图从System.Net.Mail.AlternatView的剃刀视图中分发HTML电子邮件。 I cant seem to find anything on the internet that says how to do this. 我似乎无法在互联网上找到任何说明该操作的信息。 everytihng is related to creating emails as opposed to displaying them. Everytihng与创建电子邮件(而不是显示电子邮件)有关。

can anybody help? 有人可以帮忙吗?

i have been trying things like : 我一直在尝试类似的事情:

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

im not sure how to extract the html content or how to display it though 我不确定如何提取html内容或如何显示它

thanks 谢谢

Based om this Retrieving AlternateView's of email Answer you can just have function to get stream as string 基于此检索电子邮件的AlternateView的答案您可以只具有将流作为字符串获取的功能

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

and then 接着

ViewBag.Message= string ExtractAlternateView();

 @Html.Raw(ViewBag.Message);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM