Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Sending E-Mails using ASP.NET
 
	protected void Send_Email(Object Sender, EventArgs e) {
		MailMessage mail = new MailMessage();

			mail.To = Request.Form["to"];
			mail.From = Request.Form["from"];

			if(Request.Form["format"].Equals("text"))
				mail.BodyFormat = MailFormat.Text;
			else
				mail.BodyFormat = MailFormat.Html;

			mail.Subject = Request.Form["subject"];
			mail.Body = Request.Form["body"];

		SmtpMail.Send(mail);
		Response.Flush();

		Message.Text = "Message Sent...<br><br>" + 
				"<a href=\"sendMail.aspx\">Go Back</a>";
	}

Then we close the script code block.

</script>

We have been talking of Message control but where is it and what is a control? Well, a control is a server-side component which does some thing useful. The Message control is of type ASP:Label and the only reason we are using it is that we can set it's value using logic on the server-side. We will learn more about controls in some other article later.

We declare ASP:Label control on the top of the page. Notice the value of "id" attribute? this is why we have been using Message handle to set it's value.

<p align="center" class="title">
	<asp:label id="Message" runat="server" />
</p>

Next we again look for Page.IsPostBack property and if the page has not been posted back then show the e-mail Form to the user.

<% if(!Page.IsPostBack) { %>
	... all Form code here
<% } %>

One last thing interesting is that we said that our Send_Email() method is an event method raised on the server-side when the user presses the submit button, how? we did that by putting yet another type of server-side control. In the submit button code we put two new attributes, "runat=server" to make it a control and "OnServerClick". This "OnServerClick" event is the key and it's value is our Send_Email() method. So our Send_Email() method will be called every time user presses this submit button.

	<input type="submit" OnServerClick="Send_Email" runat="server" 
		class="submit" value=" " /> Send Email!

Don't worry if you cannot swallow this server-side control and event model. We will study it in detail some other time. For now let's move forward to the next page where we run this sendMail.aspx page and see the results.

Running the sendMail.aspx page
Ok, it is time to run the sendMail.aspx page. You can use a URL like http://localhost/net/sendMail.aspx on your system, provided where you have placed the sendMail.aspx page. On my system the sendMail.aspx page looked like following :

sendMail.aspx
sendMail.aspx

Now enter your valid email address in the "From" and "To" fields, any text in the subject and body fields and hit the "Send Email!" button on the sendMail.aspx page on your system. If all goes well you should see a confirmation message like I did :

Confirmation Message
sendMail.aspx - Confirmation Message

If you don't get a confirmation message and an error message then most probably SMTP service is not enabled on your system. You can then provide your own SMTP server ( provided by your ISP ) and change the SmtpMail.Send(mail) line in the Send_Email() method to the one below :

	SmtpMail.SmtpServer = "smtp.yourisp.com";
	SmtpMail.Send(mail);

And this time you should be able to send your e-mail. Well this is it for this tutorial, one thing to keep in mind is to provide valid email address in From and To fields and non-empty values in subject and body fields. If you don't do that you'll get a nasty ASP.NET error message on your face. This is why I have called this tutorial Part I, because there is no validation for the values entered by the user. Hopefully in Part II we'll learn how to handle a case when user didn't enter a value and pressed the "Send Email!" button or he provided an invalid destination e-mail address.

	protected void Send_Email(Object Sender, EventArgs e) {
		MailMessage mail = new MailMessage();

			mail.To = Request.Form["to"];
			mail.From = Request.Form["from"];

			if(Request.Form["format"].Equals("text"))
				mail.BodyFormat = MailFormat.Text;
			else
				mail.BodyFormat = MailFormat.Html;

			mail.Subject = Request.Form["subject"];
			mail.Body = Request.Form["body"];

		SmtpMail.Send(mail);
		Response.Flush();

		Message.Text = "Message Sent...<br><br>" + 
				"<a href=\"sendMail.aspx\">Go Back</a>";
	}

Then we close the script code block.

</script>

We have been talking of Message control but where is it and what is a control? Well, a control is a server-side component which does some thing useful. The Message control is of type ASP:Label and the only reason we are using it is that we can set it's value using logic on the server-side. We will learn more about controls in some other article later.

We declare ASP:Label control on the top of the page. Notice the value of "id" attribute? this is why we have been using Message handle to set it's value.

<p align="center" class="title">
	<asp:label id="Message" runat="server" />
</p>

Next we again look for Page.IsPostBack property and if the page has not been posted back then show the e-mail Form to the user.

<% if(!Page.IsPostBack) { %>
	... all Form code here
<% } %>

One last thing interesting is that we said that our Send_Email() method is an event method raised on the server-side when the user presses the submit button, how? we did that by putting yet another type of server-side control. In the submit button code we put two new attributes, "runat=server" to make it a control and "OnServerClick". This "OnServerClick" event is the key and it's value is our Send_Email() method. So our Send_Email() method will be called every time user presses this submit button.

	<input type="submit" OnServerClick="Send_Email" runat="server" 
		class="submit" value=" " /> Send Email!

Don't worry if you cannot swallow this server-side control and event model. We will study it in detail some other time. For now let's move forward to the next page where we run this sendMail.aspx page and see the results.

Running the sendMail.aspx page
Ok, it is time to run the sendMail.aspx page. You can use a URL like http://localhost/net/sendMail.aspx on your system, provided where you have placed the sendMail.aspx page. On my system the sendMail.aspx page looked like following :

sendMail.aspx
sendMail.aspx

Now enter your valid email address in the "From" and "To" fields, any text in the subject and body fields and hit the "Send Email!" button on the sendMail.aspx page on your system. If all goes well you should see a confirmation message like I did :

Confirmation Message
sendMail.aspx - Confirmation Message

If you don't get a confirmation message and an error message then most probably SMTP service is not enabled on your system. You can then provide your own SMTP server ( provided by your ISP ) and change the SmtpMail.Send(mail) line in the Send_Email() method to the one below :

	SmtpMail.SmtpServer = "smtp.yourisp.com";
	SmtpMail.Send(mail);

And this time you should be able to send your e-mail. Well this is it for this tutorial, one thing to keep in mind is to provide valid email address in From and To fields and non-empty values in subject and body fields. If you don't do that you'll get a nasty ASP.NET error message on your face. This is why I have called this tutorial Part I, because there is no validation for the values entered by the user. Hopefully in Part II we'll learn how to handle a case when user didn't enter a value and pressed the "Send Email!" button or he provided an invalid destination e-mail address.


Previous ( 1 Gone )( No Further Pages )

See all comments and questions (post-ad) posted for this tutorial.


Related Articles
  1. File Uploading in ASP.NET Using C#
  2. Sending e-mail with attachments from an ASP.NET page
  3. Your first ASP.NET page
  4. Browser Capabilities Component in ASP.NET
  5. A Preview of Active Server Pages+
  6. Exposing Web Services
  7. Get Detailed Information About Your Site Visitors In Real Time using ASP.NET
  8. ASP.NET Website Programming: Problem - Design - Solution : Deploying the Site
  9. How to determine what server is given web site running on using ASP.NET?
  10. Sending Mass E-Mails using ASP.NET

Comments/Questions ( Threads: 16, Comments: 31 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Sending an e-mail using ASP.NET
  2. regarding sending mail
  3. not able to send email VERY URGENT!!
  4. how can i use flash file in asp.net page using vb.net tool?(answer quick)
  5. how can I save the file(picture file) or send that file with mail on ftp server from http server?
  6. how can I save the file(picture file) or send that file with mail on ftp server?
  7. i want to ask one question....
  8. not able to send email with smtp VERY URGENT!! ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  9. How to find the name of SMTP Server ( 1 Reply )
  10. SMTP authentication (reley error)
  11. Error:Could Not Access CDO.Message Object (urgent)
  12. Error:Could Not Access CDO.Message Object (urgent)
  13. Working environment required for send a mail with smtoMail ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  14. Could not access 'CDO.Message' object. ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  15. I am getting this cdo.message object error on smtpmail.mail ( 10 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  16. This article by Faisal Khan is a masterpiece! ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.