WorldofASP.NET : ASP.NET Tutorial, Hosting, and Source Code
You are 1 of 57 users

WorldofASP.NET >> ASP.NET >> General ASP.NET

Sending Mail Using Web Form Controls

How to send email with complete attribute in ASP.NET

Published Date : 12 May 2008

Author : Hans Candra
Language : C#
Platform : .NET
 
Technology : Visual Studio,Web Forms
Views : 588
Rating : (0 votes so far)
Email to a Friend | Print this Article | Add to Favourites | Report Error

Introduction

To send e-mails, you should require access to a server with .NET Framework and SMTP enabled on it. The .NET Framework supplies a SMTP class that enables you to send a simple e-mail message. If you have to send an e-mail with added functionalities, you have to make use of the MailMessage class. With the help of this class, you can insert attachments, set priorities, and much more, very easily. You can also send HTML e-mail using this class.

Main

To send an e-mail with a simple text message, you have to use the Send() method of SMTP class. The syntax of the Send() method is shown below:

SmtpMail.Send("FROM","TO","SUBJECT","MESSAGE BODY");
}

Example:

SmtpMail.Send("mymail@mymail.com","yourmail@yourmail.com",
"Test Mail Subject","This is the test mail");

You can build a User Interface using the required WebForm controls, as shown in below.

Before we going to the next step one important thing is to check/ validate your form. Always perform validation control in every form submitted is a clever way to reduce database data stroring error because of mismatch datatype, etc. In this case it is for validating an email. For Email validation method can refer to another tutorial Here.

Instead of supplying all parameters in the Send() method, you can define properties and their corresponding values separately by creating an instance of the MailMessage class. With the help of this class, you can easily add attachments, set priorities, BCC, CC values, and much more. Table given at the end of this article, shows a list of properties of the MailMessage class. Add the code given below by double-clicking the button captioned Submit:

MailMessage objEmail          = new MailMessage();
            objEmail.To       = txtTo.Text;
            objEmail.From     = txtFrom.Text;
            objEmail.Cc       = txtCc.Text;
            objEmail.Subject  = "Test Email";
            objEmail.Body     = txtName.Text + ", " +
                                txtComments.Text;
            objEmail.Priority = MailPriority.High;
  //SmtpMail.SmtpServer = "localhost";
  try{
    SmtpMail.Send(objEmail);
    Response.Write("Your Email has been sent sucessfully -
                    Thank You");
  }
  catch (Exception exc){
    Response.Write("Send failure: " + exc.ToString());
  }

Property Description
Attachments Used for sending e-mails with attachments
From Sender's e-mail address
To Recipient's e-mail address
Cc Recipient's e-mail address (Carbon Copy)
Bcc Recipient's e-mail address (Blind Carbon Copy)
Body Text of the e-mail message
BodyFormat Specifies the format of an e-mail message (Possible Values: Text, Html)
Priority Specifies the priority of an e-mail message (Possible Values: High, Low, and Normal)
Subject Denotes the subject of an e-mail message
Headers Denotes a collection of acceptable headers (Example: Reply-To)
BodyEncoding Specifies the method of encoding an e-mail message (Possible Values: Base64 and UUEncode)


Below sytax is used for sending your mail in HTML format:

objEmail.BodyFormat = MailFormat.Html;

Conclusion

To send an email from your ASP.NET page, you need to: import the System.Web.Mail namespace in your ASP.NET page, create an instance of the MailMessage class, set all the properties of the MailMessage instance, and send the message with SmtpMail.Send method.

References

All the useful links or references that can help you learn about this tutorial

  1. SMTP Client Class
  2. Configure SMTP Server and Email



      Other Related and Popular Articles :

      Working with Master Pages and Basics of Master Pages
      Why do you need master pages and how it can improve the maintenance of your website
      Tips to Improve Your ASP.NET Web site performance
      The article contains guidelines for improving your ASP.NET Web applications
      Use C# and VB.NET in the same project
      Use C# and VB.NET classes in the same ASP.NET application
      Publish and create RSS Feeds easily on your ASP.NET websites
      This article explains the concept of creating RSS feeds for your ASP.NET website with auto updating contents
      Working with HttpWebRequest and HttpWebResponse in ASP.NET
      This article explain how to use WebRequest and WebResponse in ASP.NET to grab contents and screen scrape
      Sending Email in ASP.NET 1
      This article explain how to send email in ASP.NET 1 either by using SMTP authentication or not

      Author Profile : Hans Candra

      Click here to view Author Profile


      How would you rate the quality of this content?
      Poor Excellent

      Comments

      Leave New Comments


      Article Content copyright by Hans Candra
      Everything else Copyright © by WorldofASP.NET 2008
       
      Announcements
      Earn Cash by writing an article or review
      For more info Click here







      Legend : - Within 3 Days - Within 6 Days - Within 9 Days

      Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 Link To Us | Contact Us
      © 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
      Our Partners : ASP.NET Web Hosting | Windows Web Hosting | FREE ASP.NET CMS | Phone Card | PHP Directory | Bangkok Hotels |Calling Card