One of the common problems that I have seen developer facing is sending a web page as attachment.I have come across this solution that will embed complete web page into your mail as body.Sending attachment is a very easy task with mail
First of all we will download a webpage using code.There are classes exposed in System.Net namespace through which yo can upload and download entire webpages without opening internet explorer or any other browser.One of such classes is WebRequest class.
Create an object of WebRequest class passing it the URL pf page to attach.This will make a request for the webpage via code.
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
To get the response from the request call GetResponse() method on it which returns a WebResponse class object but here instead of assigning it to an object of WebResponse class we will call GetResponseStream() method on it to get the entire page stream.
StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());
Why StreamReader?
Because it exposes methods to read entire content as string.With other streams we need to make conversions from byte to string.
string result = sr.ReadToEnd();
Once we have the entire page content as string we will create a MailMessage as
MailMessage mail = new MailMessage("From", "TO");
mail.Subject = "this is a test email.";
string url = "http://www.microsoft.com";
mail.Body = HttpContent(url);
mail.IsBodyHtml = true;
here we have set the IsBodyHtml property to true to indicate that we will passing html as body of the message.Rest is simple create an object of SmtpClient and call send method on in passing the message just created.
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.Send(mail);
don’t forget to set EnableSsl property to true in case your smtp server needs SSL.In this particular sample we are making use of gmail’s SMTP server which requires SSL.
Here is the complete code
private void Page_Load(object sender, System.EventArgs e)
{
MailMessage mail = new MailMessage("From", "TO");
mail.Subject = "this is a test email.";
string url = "http://www.microsoft.com";
mail.Body = HttpContent(url);
mail.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.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;
}
And here goes the web.config settings
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com" port="587" userName="yourgmailId" password=" yourgmailPassword "/>
</smtp>
</mailSettings>
</system.net>
First of all we will download a webpage using code.There are classes exposed in System.Net namespace through which yo can upload and download entire webpages without opening internet explorer or any other browser.One of such classes is WebRequest class.
Create an object of WebRequest class passing it the URL pf page to attach.This will make a request for the webpage via code.
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
To get the response from the request call GetResponse() method on it which returns a WebResponse class object but here instead of assigning it to an object of WebResponse class we will call GetResponseStream() method on it to get the entire page stream.
StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());
Why StreamReader?
Because it exposes methods to read entire content as string.With other streams we need to make conversions from byte to string.
string result = sr.ReadToEnd();
Once we have the entire page content as string we will create a MailMessage as
MailMessage mail = new MailMessage("From", "TO");
mail.Subject = "this is a test email.";
string url = "http://www.microsoft.com";
mail.Body = HttpContent(url);
mail.IsBodyHtml = true;
here we have set the IsBodyHtml property to true to indicate that we will passing html as body of the message.Rest is simple create an object of SmtpClient and call send method on in passing the message just created.
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.Send(mail);
don’t forget to set EnableSsl property to true in case your smtp server needs SSL.In this particular sample we are making use of gmail’s SMTP server which requires SSL.
Here is the complete code
private void Page_Load(object sender, System.EventArgs e)
{
MailMessage mail = new MailMessage("From", "TO");
mail.Subject = "this is a test email.";
string url = "http://www.microsoft.com";
mail.Body = HttpContent(url);
mail.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.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;
}
And here goes the web.config settings
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com" port="587" userName="yourgmailId" password=" yourgmailPassword "/>
</smtp>
</mailSettings>
</system.net>
No comments:
Post a Comment