These few lines will allow you to email as html an aspx page from your site or in fact any web page you want (though i wouldnt recommend any with lots of scripts etc in it). It is very useful for email forms to customers etc from your site.

Personally I create an plainer version of the form just for email. I remove any menu functions and other things that are there as part of the website and are not useful in the email - but that is a personal preference.

I have also created an emailer class which i have simplified here for you

using System.Net.Mail;

public static class Emailer
public static void SendEmail(string from, string to, string subject, sting body)
SmtpClient smtp = new SmtpClient("hostname");
MailMessage email = new MailMessage(from, to, subject, body);
email.IsBodyHtml = true;

Now to send the page. Remember it can be any page and so you can do any codebehind processing first that you wish. In essence we call Server.Execute which runs the page as if it were in a browser, but without opening the page in the browser for the user to see. The lines of code are simple as follows:

using System.Net;
using System.IO;

////*** somewhere in an aspx page - preferably a different one to the one you are emailing but doesnt have to be******
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Server.Execute("theaspxpage.aspx", htw);
//the page has now processed and the markup is in the textwriter inner text which is the stringwriter
//I also set a status in the called aspx page and check to see all was ok before continuing but will keep it simple here
Emailer.SendEmail("from@here.com", "to@there.com", "Your aspx page", sw.ToString());

You could pass parameters in the header of server.execute if needed or anything just as calling a normal aspx page. It you wanted to call a third party page instead then you cant use Response.Redirect as that will send the browser to the page. You can use the HttpWebRequest to do it for you. I wont go into that in this tutorial - maybe another day.

Remember if you want to place the code above into the page you want to email then do not put it in the page load without some form of checking (say a flag) otherwise you will go into an neverending loop and overflow error.

11 Years
Discussion Span
Last Post by sadaffahad

Hi f1,
The thread posted by you was really helpful. But in my case I am emailing an aspx page which has a submit button. I need to capture this even and code accordingly. Please let me know how can I code on the button present in the email.



this helps me lot! But, what to do if i need to read only Specific DIV element contents from whole page returned string?

Edited by Shekhar_2


An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

i use your suggested code but had error above :(

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.