views:

136

answers:

3

I have a HTML page that I have created that essentially consists of:

<div>
   <table>
       <tr>
          <td><asp:label runat="server" id="someText"></asp:label></td>
       </tr>
   </table>
</div>

The label text is generated on page load from an SQL query.

The above is a very basic and simplified version of what I have on my page.

What I'd like to achieve is to be able to e-mail the entirety of the rendered HTML page without having to build the page again in my code-behind to send it.

Is there a way of doing this?

Thanks in advance for any help.

A: 

Use a webclient:

Dim wc As New WebClient
Dim str As String = wc.DownloadString("yoururl.com")
SendEmail(str) ' your email method '
Jason
would this not work?
Jason
+1 this would work, if the page is valid html, email clients are picky about the HTML in the body of an email... (css and js files are usually not too well accepted...
BigBlondeViking
+4  A: 

Something like this maybe (haven't tested):

StringBuilder sb = new StringBuilder();
HtmlTextWriter hw = new HtmlTextWriter(new System.IO.StringWriter(sb));

this.Render(hw);

MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.Body = sb.ToString();

(new SmtpClient()).Send(message);
John
+1  A: 

If you want to send the email as you render the page, without rerunning the page lifecycle, try the following.

Make a wrapper stream class that inherits Stream and contains two additional streams, and override its Write method to write to both streams. I don't think you need to override anything else except the Can_Whaterver_ properties, but I'm not sure.

Make a field in your Page class of type MemoryStream to hold a copy of the response.

Then, handle the page's PreInit event and set the Response.Filter, like this:

Response.Filter = new CopierStream(Response.Filter, responseCopy);
//`CopierStream` is the custom stream class; `responseCopy` is the MemoryStream

Finally, override the Page's Render method, and, after calling base.Render, you can send responseCopy by email using the SmtpClient class.

This is a very complicated technique that you should only do if you really don't want to re-run the page lifecycle.


Whichever way you do it, if your page has any images or hyperlinks, make sure that their urls include the domain name, or else they won't work in the email.

SLaks