Tutorials
Resources
Forum
Communities
Interview
Jobs
Projects
Offshore Development
Silverlight Tutorials
|
Mentor
|
Code Converter
|
Articles
|
Code Factory
|
Computer Jokes
|
Members
|
Peer Appraisal
|
IT Companies
|
Bookmarks
|
Revenue Sharing
|
Prizes & Awards
My Profile
Sign In
Register
AdSense Revenue
Active Members
Today
Abhi
(38)
divya
(34)
Babu Akkandi
(28)
Last 7 Days
Appukuttan
(620)
Babu Akkandi
(465)
Tejinder Singh ...
(273)
more...
New Feature:
Community Sites
:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
Sending a web Page as mail body using Gmail server
Posted Date: 03 Jun 2008
Resource Type:
Articles
Category:
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
(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:
Deploying Pre-Compiled Web Applications
Return to Discussion Resource Index
Post New Resource
Category:
Web Applications
Post resources and
earn money
!
Related Resources
Should you Use DataSet or DataReader
Adding common functionality with user controls
Page Directives
How to add service reference dynamically from user control? Microsoft Ajax
Customize Datagrid - Series 2 - Paging
dotNet Slackers
BizTalk Adaptors
Web Design
Contact Us
Privacy Policy
Terms Of Use