How to send a web page in Email ?


System.Web.Mail does not support sending a web page through Email but we can use WebRequest class to get screen scrape web pages, and we can pass the resulting Html string to the MailMessage class.
Here is step by step Guidline how will you achieve that goal.



Add the following namespace
using System.Net;
using System.IO;
using System.Net.Mail;

private string getPageHtml(string url)
{
WebRequest objCreateReq = System.Net.HttpWebRequest.Create(url);
StreamReader ObjSreader = new StreamReader(objCreateReq.GetResponse().GetResponseStream());
string PageHtml = ObjSreader.ReadToEnd();
ObjSreader.Close();
return PageHtml;
}

protected void sendMail()
{
MailMessage Mymail = new MailMessage();
Mymail.To.Add("toEmailhere");
Mymail.From = new System.Net.Mail.MailAddress("yourEmailhere");
Mymail.Subject = "this is a test email to send a web page in email.";
string url = "http://www.microsoft.com/en-us/default.aspx";
Mymail.Body = getPageHtml(url);
Mymail.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
//Add the your gmail id and password
client.Credentials = new System.Net.NetworkCredential("gmailEmailID", "GmailPassword");
client.Port = 587; // this is the port where gmail work
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Send(Mymail);

}

Call the send email function to send email
protected void Page_Load(object sender, EventArgs e)
{
sendMail();
}


if you have any problem then please feel free to write comment thanx.
Tags:

Join Us!