views:

113

answers:

2

Hello,

I have a stream loaded with HTML that I got from an export. I can take this html from the stream in a byte[] (is there any other way?), and I want to write this byte[] to a html mail body.

The reason to do this is that I want to export a report to html, and use this data in the body of the mail, instead of exporting it to pdf and send it as an attachment.

Hope it's clear enough, let me know if I need to add something.

Thanks

+1  A: 

Hey,

I found it. It's very simple, although I didn't find the result I would like, having a formatted output directly to the mail, the question was more about how to use the bytes stored in a stream to output html.

I take the stream and create a StreamReader:

            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);

Then, in the body of the mail object, I read the contents of the stream using the StreamReader:

mail.Body = reader.ReadToEnd();

That's all. What do you think?

Sebastian
A: 

You can use a Multipart Mime message and pass the stream to the AlternateView constructor as well, just don't close the stream or clean it up until after you've sent the message (call MailMessage.Dispose() to handle cleanup for you if you like). If you go with your solution, be sure to set mail.IsBodyHtml to true in order for the Content Type to get set correctly otherwise the receiving mail client may not display the HTML as HTML since it will think it's plain text. If the output of the mail looks funny in your mail client then that's probably why.

The code would look something like this (truncated to only the relevant parts):

MailMessage message = new MailMessage();
message.AlternateViews.Add(new AlternateView(stream, new ContentType("text/HTML"));
// do NOT set Body, IsBodyHtml, or ContentType on MailMessage or 
// you'll mess up the mime types

smtpClient.Send(message);
Jeff Tucker