<<Back To Faq 3.2


Complete FAQ Listing


3.2 How do I send a web page? Printer Friendly   Email This FAQ   Discuss

System.Web.Mail does not natively support sending a web page. However, using the WebRequest class, you can screen scrape web pages, and pass the resulting Html string to the MailMessage class. The following example demonstrates this technique.

Note: Be sure to import the System.Net and System.IO namespaces for this code snippet.
 
[ C# ]
private void Page_Load(object sender, System.EventArgs e)
{
	MailMessage mail = new MailMessage();
	mail.To = "me@mycompany.com";
	mail.From = "you@yourcompany.com";
	mail.Subject = "this is a test email.";
	string url = "http://www.microsoft.com";
	mail.Body = HttpContent( url );
	mail.BodyFormat = MailFormat.Html;
	mail.UrlContentBase = url;
	SmtpMail.SmtpServer = "localhost";  //your real server goes here
	SmtpMail.Send( mail );
}
//screen scrape a page here
private string HttpContent( string url )
{
	WebRequest objRequest= System.Net.HttpWebRequest.Create(url);
	StreamReader sr =  new StreamReader( objRequest.GetResponse().GetResponseStream() );  
	string result = sr.ReadToEnd();
	sr.Close();
	return result;
}

[ VB.NET ]
Private Sub Page_Load(sender As Object, e As System.EventArgs)
   Dim mail As New MailMessage()
   mail.To = "me@mycompany.com"
   mail.From = "you@yourcompany.com"
   mail.Subject = "this is a test email."
   Dim url As String = "http://www.microsoft.com"
   mail.Body = HttpContent(url)
   mail.BodyFormat = MailFormat.Html
   mail.UrlContentBase = url
   SmtpMail.SmtpServer = "localhost" 'your real server goes here
   SmtpMail.Send(mail)
End Sub 'Page_Load


'screen scrape a page here
Private Function HttpContent(url As String) As String
   Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(url)
   Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
   Dim result As String = sr.ReadToEnd()
   sr.Close()
   Return result
End Function 

 



The formatted version of this faq can be found at http://www.SystemWebMail.com/faq/3.2.aspx