Paul
Paul

Reputation: 705

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. 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.

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

thanks

Upvotes: 2

Views: 2778

Answers (1)

COLD TOLD
COLD TOLD

Reputation: 13579

Based om this Retrieving AlternateView's of email Answer you can just have function to get stream as string

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

Upvotes: 1

Related Questions