System.Web.Mail FAQ

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 

 

Discuss This FAQ Item
Got a question? Another Suggestion? Want to post your solution? Discuss it below.
New Post
  • url as meessage content    [ Reply ]
    • RE: url as meessage content    [ Reply ]
    • RE: url as meessage content    [ Reply ]
    • RE: url as meessage content    [ Reply ]
  • Code above issues with some sites.    [ Reply ]
  • Code above issues with some sites.    [ Reply ]
  • How to send the url as a link!    [ Reply ]
  • Sending a page containing sessions variables    [ Reply ]
    • RE: Sending a page containing sessions variables    [ Reply ]
      • RE RE: Sending a page containing sessions variables    [ Reply ]
        • RE: RE RE: Sending a page containing sessions variables    [ Reply ]
  • Pictures are not Available in Mail    [ Reply ]
    • RE: Pictures are not Available in Mail    [ Reply ]
  • bad url and exception    [ Reply ]
    • RE: bad url and exception    [ Reply ]
  • System.Net.WebException: The remote server returned an error: (401) Unauthorized.    [ Reply ]
    • RE: System.Net.WebException: The remote server returned an error: (401) Unauthorized.    [ Reply ]
      • RE RE: System.Net.WebException: The remote server returned an error: (401) Unauthorized.    [ Reply ]
  • Error in mailing a webpage    [ Reply ]
  • send page    [ Reply ]
    • RE: send page    [ Reply ]
  • send web page    [ Reply ]
    • RE: send web page    [ Reply ]
  • sending asp pages    [ Reply ]
  • sending asp pages    [ Reply ]
  • sending asp pages through outlook    [ Reply ]
  • html sent throw asp.net dyanamic page    [ Reply ]
  • Problem when sending    [ Reply ]
    • RE: Problem when sending    [ Reply ]
      • RE RE: Problem when sending    [ Reply ]
  • Mail a WEb page    [ Reply ]
  • Email Wtih Attachments    [ Reply ]
  • button with System.Net.Mail    [ Reply ]
    • RE: button with System.Net.Mail    [ Reply ]
  • mailbox    [ Reply ]