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();
}
{
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.
Sending Email is a basic task of a web application for many purposes . If you have any web space somewhere on a web server then you are provided with the Email accounts and SMTP and POP services for sending and receiving E-mails.
If you want to send email using your Gmail account or using Gmail's smtp server in ASP.NET application or if you don't have a working smtp server to send mails using your ASP.NET application or aspx page then sending e-mail using Gmail is best option.
But Gmail allow to send mail to limited number of people per day and you can’t send same email to unlimited number of people Gmail suppose it as spam message.
So if you want to send mail to limited number of people from asp.net page then sending mail through gmail will be best option.
Here I am showing the step by step process .
1-add a new web application in visual studio and add a Web form to it.And add a button on this page.We will use this buttong click event to send Email.
2-Add NameSpace System.Net.Mail by using this
using System.Net.Mail;
3-on the button click event add this code
Replae to and from email with the real email otherwise code will not work.
If you want to send email using your Gmail account or using Gmail's smtp server in ASP.NET application or if you don't have a working smtp server to send mails using your ASP.NET application or aspx page then sending e-mail using Gmail is best option.
But Gmail allow to send mail to limited number of people per day and you can’t send same email to unlimited number of people Gmail suppose it as spam message.
So if you want to send mail to limited number of people from asp.net page then sending mail through gmail will be best option.
Here I am showing the step by step process .
1-add a new web application in visual studio and add a Web form to it.And add a button on this page.We will use this buttong click event to send Email.
2-Add NameSpace System.Net.Mail by using this
using System.Net.Mail;
3-on the button click event add this code
string Emailfrom = from@gmail.com; //Replace this with your own Gmail id
string Emailto = to@gmail.com //Replace this with the Email Address where you want to send the Email
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(Emailto);
mail.From = new MailAddress(Emailfrom, "hello" , System.Text.Encoding.UTF8);
mail.Subject = "Put your Email subject Here" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "put your message here thet you want to sent";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//Add the your gmail id and password
client.Credentials = new System.Net.NetworkCredential(Emailfrom, "Password");
client.Port = 587; // this is the port where gmail work
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
HttpContext.Current.Response.Write(errorMessage );
}
string Emailto = to@gmail.com //Replace this with the Email Address where you want to send the Email
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(Emailto);
mail.From = new MailAddress(Emailfrom, "hello" , System.Text.Encoding.UTF8);
mail.Subject = "Put your Email subject Here" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "put your message here thet you want to sent";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//Add the your gmail id and password
client.Credentials = new System.Net.NetworkCredential(Emailfrom, "Password");
client.Port = 587; // this is the port where gmail work
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
HttpContext.Current.Response.Write(errorMessage );
}
Replae to and from email with the real email otherwise code will not work.