Email an aspx page (or any webpage you want)

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2006
Posts: 275
Reputation: f1 fan is an unknown quantity at this point 
Solved Threads: 11
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Email an aspx page (or any webpage you want)

 
0
  #1
Jan 13th, 2006
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
  1. using System.Net.Mail;
  2.  
  3. public static class Emailer
  4. {
  5. public static void SendEmail(string from, string to, string subject, sting body)
  6. {
  7. SmtpClient smtp = new SmtpClient("hostname");
  8. MailMessage email = new MailMessage(from, to, subject, body);
  9. email.IsBodyHtml = true;
  10. smtp.Send(email);
  11. }
  12. }

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:

  1. using System.Net;
  2. using System.IO;
  3.  
  4. ////*** somewhere in an aspx page - preferably a different one to the one you are emailing but doesnt have to be******
  5. StringWriter sw = new StringWriter();
  6. HtmlTextWriter htw = new HtmlTextWriter(sw);
  7. Server.Execute("theaspxpage.aspx", htw);
  8. //the page has now processed and the markup is in the textwriter inner text which is the stringwriter
  9. //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
  10. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1
Reputation: abhishek_itengg is an unknown quantity at this point 
Solved Threads: 0
abhishek_itengg abhishek_itengg is offline Offline
Newbie Poster

Re: Email an aspx page (or any webpage you want)

 
0
  #2
Dec 9th, 2007
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.

Regards,
Abhishek
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC