TutorialsRoom.com
RSS Feed

Subscribe: RSS or e-mail

Home » Web Development » ASP.NET »

Sending Email With ASP.Net

Sending Email With ASP.Net
In today’s tutorial, we will find out how easy it is to send email using ASP.Net and just a few lines of code!

Sending email with ASP.Net is an easy task thanks to the classes in System.Net.Mail namespace So, Open your Visual Studio and let's send our first email!

In your ASP.Net page, add some controls and lay them out like the following:

Send Email

The layout is really easy to understand, give your controls meaningful IDs. For the last TextBox, you need the set the TextMode property to MultiLine for the email body text. Of course in real life applications you will need to add some ASP.Net Validation Controls but we will keep it simple in this tutorial.

Double click on the Send button to write the btnSend_Click code, but first we need to use the System.Net.Mail namespace so type using System.Net.Mail; at the top of your page.

In the the btnSend_Click type this simple code:
  MailMessage email = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
  SmtpClient smtp = new SmtpClient();
  //Replace "MyUserName", "MyPassword" with your real username and Password
  System.Net.NetworkCredential cred = new System.Net.NetworkCredential("MyUserName", "MyPassword");
  //Replace "MySMTPServer" with your real SMTP server
  smtp.Host = "MySMTPServer";
  //Replace 26 with your SMTP port
  smtp.Port = 26;
  smtp.Credentials = cred;
  smtp.Send(email);

Those simple lines are all you need to send an email using ASP.Net! Now let's explain the code line by line.

First, we need to declare a new instance of the MailMessage class, this will hold the From email, the To email, the Subject and the Body.

To be able to send email, you need to have SMTP server (i.e. Simple Mail Transfer Protocol Server) which will send the email for you. You can use the SMTP server provided to you by your host. Most SMTP servers come in the form of mail.servername.com (e.g. mail.gmail.com). The SMTPClient class is the class that will hold the SMTP server data and send the email.

To be able to login to your SMTP server, you need at least a username and password, this is called authentication data or credentials, so we create a System.Net.NetworkCredential object and provide it with the username and password of the SMTP server. Notice that for most of the shared hosting environments, the username is the email that you create using your CPanel or whatever software your hosting provider uses for creating new emai accounts.

The other lines are easy to understand, just make sure to type the SMTP port correctly because many hosting providers change the default port (25) because many firewalls and proxies block it due to spam.
Rating: 2.5/5 (68 Votes)
3 Comments
June 23rd, 2011

tutorialsroom is super ! thanks to posting this..Be continue

pranoti said,
June 6th, 2011

hey dis was good

sailor said,
March 8th, 2010

wow you helped me a lot huh, saved me reseach time

Post Your Comment (In English):



(Optional, will not be shown)


(Optional)




Please notice that comments which are not related and those with live links will be deleted.

Copyright © 2007-2013 Hazem Osman. All rights reserved. Terms & Conditions