Reputation: 705
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
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