| | |
Email an aspx page (or any webpage you want)
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
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
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:
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.
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
ASP.NET Syntax (Toggle Plain Text)
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; smtp.Send(email); } }
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:
ASP.NET Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- How to disable Back button of IE when executing welcome.aspx page? (ASP.NET)
- how to make the aspx page at runtime (ASP.NET)
- Create .aspx page at runtime (ASP.NET)
- aspx page not loading!!!! Method called too soon (C#)
- .aspx page won't open at home (ASP.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: Simple .net changes
- Next Thread: Validator in GridView
| Thread Tools | Search this Thread |
.net 2.0 3.5 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox click commonfunctions compatible confirmationcodegeneration content contenttype countryselector courier css dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dropdownmenu edit expose flash flv formatdecimal forms formview gridview homeedition iframe iis javascript jquery list listbox login menu microsoft mono mouse mssql multistepregistration nameisnotdeclared news numerical objects order panelmasterpagebuttoncontrols problem radio ratings reportemail rotatepage save schoolproject search security serializesmo.table silverlight smartcard software sql-server sqlserver2005 suse textbox tracking unauthorized validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webapplications webarchitecture webdevelopemnt webprogramming webservice xml youareanotmemberofthedebuggerusers





