Tutorials
Resources
Forum
Reviews
Communities
Interview
Jobs
Projects
Training
Your
Ad
Here
Silverlight Games
|
Mentor
|
Code Converter
|
Articles
|
Code Factory
|
Computer Jokes
|
Members
|
Peer Appraisal
|
IT Companies
|
Bookmarks
|
Polls
|
Revenue Sharing
|
Lobby
|
Gift Shop
|
Prizes & Awards
My Profile
Sign In
Register
AdSense Revenue
Active Members
Today
Alwyn
(200)
Asheej T K
(114)
Avinash Mohan
(90)
Last 7 Days
Alwyn
(938)
Asheej T K
(537)
nishithraj
(525)
more...
Resources
»
Articles
»
ASP.NET/Web Applications
»
Sending a web Page as mail body using Gmail server
Posted Date: 03 Jun 2008
Resource Type:
Articles
Category:
ASP.NET/Web Applications
Author:
shakti singh tanwar
Member Level:
Diamond
Rating:
Points
: 50
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>
Responses
Author:
Mahesh Raj
07 Jun 2008
Member Level:
Gold
Points
: 1
This is very good information,Continue posting such useful articles.
Feedbacks
Popular Tags
What are tags ?
Search Tags
Sign In
to add tags.
(No tags found.)
Post Feedback
This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must
Sign In
to post a response.
Next Resource:
cookies in asp.net
Previous Resource:
Cross Page PostBack
Return to Discussion Resource Index
Post New Resource
Category:
ASP.NET/Web Applications
Post resources and
earn money
!
More Resources
cookies in asp.net
Configuring the file in asp.net
Authentication and Security Mechanisms in Web Applications
Datagrid Inside the Datagrid
Application Domain
Encrypting Web.config in ASP.NET 2.0 Applications:
dotNet Slackers
About Us
Contact Us
Privacy Policy
Terms Of Use