Add a new post


Name  
Email*  
Subject  
Message
 
 
Protected by Clearscreen.SharpHIPTo prevent abuse from bots, please enter the text you see.:
 

* In order to reduce SPAM, all email addresses will be obfuscated so they cannot be read by spam bots.
For example: 'test@test.com' will become 'test (at) test dot com'.

How do I send a web page?
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