Click here to Skip to main content
12,550,449 members (44,484 online)
Click here to Skip to main content
Add your own
alternative version


96 bookmarked

Email System Using ASP.NET

, 15 Nov 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
The article explores the SMTP concepts, configuration and simple mail service.


This article explores the features of an email system developed in a simple manner and shows how to create a simple mail service without taking much effort. I am confident that whoever reads this article will get a fairly good idea about the email service and the concepts behind the mail server which is based on SMTP.

The problem

How often you come across problems that are caused due to the lack of technical knowledge and the core concepts behind the framework. This article explores the concepts behind the mail server in a most simple manner. The article:

  • Explores the configuration settings of a mail server using SMTP.
  • Describes the code in a very interesting and simple manner.
  • Describes the settings to be done for hosting it in a web server.
  • Describes the solutions to the problems that occur during runtime.

Exploring the technology

SMTP stands for Simple Mail Transfer Protocol. The emails are sent using this perfect protocol. The .NET Framework supplies the SMTP class that enables you to send simple e-mail messages. 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, etc. very easily. You can also send HTML e-mail using this class.

Pre-requisites for the mail server

First of all, you have to check whether the SMTP service is enabled in the IIS which is the Internet Information service (it's a Windows component shipped with the operating system). You can check this service in the Add/Remove section seen in the control panel:

Click the Windows Component in the Add/Remove Programs section and you will get the screen shown below:

See whether IIS is checked or not, if it is checked (installed) click IIS (Internet Information Services) and you will get the screen shown below:

Then, check whether the SMTP service is installed or not. If the service is not available, you have to select the SMTP service and follow the wizard. Sometimes it may ask for the Windows XP CD for installing the Windows component. Now the SMTP mail service is available for the application.

Configuration and settings

For configuring the SMTP server you have to follow the steps given below:

  1. Go to>Start Menu>Control Panel>Administrator Tools>Internet Information Services.
  2. Click the Internet Information Services and locate the Default SMTP Virtual Server.
  3. Right click the same and go to the properties as seen below:

    Do the settings as per the figure by selecting your own IP address.

  4. Then go to the Access option in the tab menu and click Relay Restrictions. In Relay you will see the screen shown below and now configure accordingly:

    In the Access tab you have to configure the connection property also, as per the figure shown below and leave all the other settings as it is:

    The IP address represents the localhost. Now you are ready for the application.

Code snippets

The code snippet for this project is explained below:

private void SendMail()
     MailMessage Email=new MailMessage(); //Creating the Mail Message 
     Email.To=txtTo.Text;                 //Storing the To value in 
                                          //the object.
     Email.From=txtFrom.Text;             //Storing the From value 
                                          //in the object.
     Email.Cc=txtCC.Text;                 //Storing the CC value in 
                                          //the object.
     Email.Bcc=txtBCC.Text;               //Storing the BCC value in 
                                          //the object.
     Email.Subject=txtSubject.Text;       //Storing the Subject value 
                                          //in the object.
     Email.Body=txtMessage.Text;          //Specifies the email body.
     Email.Priority=MailPriority.High;    //Setting priority to the mail 
                                          //as high,low,or normal
     Email.BodyFormat=MailFormat.Text;    //Formatting the mail as html 
                                          //or text.

     //Checking whether the attachment is needed or not.
       //Adding attachment to the mail.
                  new MailAttachment(FileBrowse.Value));

     //Specifying the real SMTP Mail Server.
     SmtpMail.Send(Email);                //Sending the mail.
     Reset();                             //calling the reset method to 
                                          //erase the text entered.
         "*Your email has been sent successfully-Thank You";
   //Handling the Exception
   catch(Exception exc)
     lblMessage.Text="Send error:"+exc.ToString();

Call the SendMail() method in the Click event of the button as shown below:

private void btnSend_Click(object sender, System.EventArgs e)

The code block in the Reset() method is given below:

private void Reset()
   //Seeking for the controls in the active webform.
   Control myForm=Page.FindControl("Form1");
   //Iterating each control.
   foreach(Control ctl in myForm.Controls)
      // Checking whether it is a text box 
      // and clear when it is a textbox

Call the Reset() method in another Click event of the button as shown below:

private void btnCancel_Click(object sender, System.EventArgs e)

Final solution

Our code now looks like this:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
namespace Email
    /// <span class="code-SummaryComment"><summary>

Using the solution

Download the source code files given above in the zip format and copy the contents into the root directory (e.g. C:\Inetpub\wwwroot). Go to the Internet Information Services and create a virtual directory named EmailSystem and give the correct path for mapping.

Solutions for some problems

The Error "Could not access 'CDO.Message' object"


Specify a valid mail server for the SmtpMail.SmtpServer property. If that property is not set, set it to or localhost. For example: SmtpMail.SmtpServer = "".


If you are using "localhost" or "" as the SmtpMail.SmtpServer, you may not have permissions to relay through the IIS SMTP service. To allow access, open up IIS MMC in the Administrator tools option. Seek the SMTP virtual server, and right-click, then select the properties. On the Access tab, click the Relay Restriction button. In the Relay Restrictions dialog, grant your IP address ( to the computer ListBox. Close all the dialogs, and restart the SMTP service again.


If you are using "localhost" or "" as the SmtpMail.SmtpServer, make sure that Anonymous access is allowed. To allow Access, open up IIS Admin MMC. Locate the SMTP virtual server, and right-click, then select the properties. On the Access tab, click the Authentication button. Then make sure "Anonymous Access" is the only checkbox checked. Close all the dialogs, and restart the SMTP service.

The transport failed to connect


The above error is a network related error. Now, make sure the server System.Web.Mail is executing. Some times firewalls or proxy servers can cause problems. Try specifying the value by IP address. Poor DNS resolution can sometimes hinder name lookups. Check that the mail server is running at port 25. If you did not specify a SmtpMail.SmtpServer property, or if SmtpMail.SmtpServer points to "localhost" (or, make sure the SMTP service is running on port 25. Sometimes the problem arises due to relay issues.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sujith John Thomas
Team Leader
India India
Sujith John Thomas, an accomplished Software Engineer specializing in Object-Oriented Design and Analysis with extensive experience in full cycle of software development,having good exposure in C#,ASP.NET,XML,AJAX,Web Service,Java Script and SQL Server .Currently developing web applications and windows applications in .NET 2.0/3.5 framework.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
rohitm09530-Nov-12 5:57
memberrohitm09530-Nov-12 5:57 
Questionsundar Pin
S.SUNDAR24-Sep-12 19:19
groupS.SUNDAR24-Sep-12 19:19 
QuestionThanks Pin
Sriramdeve24-Sep-12 18:52
memberSriramdeve24-Sep-12 18:52 
Questionhow can i create an email account Pin
KhanFaiz9-Sep-12 20:00
memberKhanFaiz9-Sep-12 20:00 
GeneralMy vote of 5 Pin
trivenivagicherla27-Aug-10 2:47
membertrivenivagicherla27-Aug-10 2:47 
GeneralRe: My vote of 5 Pin
Sujith John Thomas22-Nov-10 10:59
memberSujith John Thomas22-Nov-10 10:59 
GeneralMy vote of 4 Pin
patilrohit16-Jul-10 21:08
memberpatilrohit16-Jul-10 21:08 
QuestionHow to check SMTP IP address is valid ? Pin
trushitshah11-Feb-10 0:45
membertrushitshah11-Feb-10 0:45 
GeneralAttachment Pin
nikolas812-May-09 10:28
membernikolas812-May-09 10:28 
Questionone question, how can I get mail list which I sent? Pin
edidu3-Feb-09 20:27
memberedidu3-Feb-09 20:27 
QuestionSave mail to disk Pin
StonePit12-Jun-08 4:46
memberStonePit12-Jun-08 4:46 
GeneralCan't Find File Pin
Member 40091643-Jun-08 3:21
memberMember 40091643-Jun-08 3:21 
Generalit cant work, I have query Pin
Dipak Salve11-Feb-08 1:49
memberDipak Salve11-Feb-08 1:49 
GeneralRe: it cant work, I have query Pin
Sujith John Thomas24-Mar-08 19:34
memberSujith John Thomas24-Mar-08 19:34 
QuestionVista problem Pin
Emperor.for.earth8-Apr-07 17:04
memberEmperor.for.earth8-Apr-07 17:04 
AnswerRe: Vista problem Pin
Sujith John Thomas28-Jun-07 10:26
memberSujith John Thomas28-Jun-07 10:26 
GeneralRe: Vista problem Pin
Saksida Bojan19-Oct-07 20:13
memberSaksida Bojan19-Oct-07 20:13 
QuestionHi Pin
Member #356933928-Feb-07 1:59
memberMember #356933928-Feb-07 1:59 
AnswerRe: Hi Pin
Sujith John Thomas16-Jul-07 1:19
memberSujith John Thomas16-Jul-07 1:19 
GeneralRe: Hi Pin
shoukat Ali23-Mar-08 23:06
membershoukat Ali23-Mar-08 23:06 
GeneralRe: Hi Pin
Sujith John Thomas24-Mar-08 19:01
memberSujith John Thomas24-Mar-08 19:01 
GeneralMAPI Pin
DR Van20-Dec-06 14:53
memberDR Van20-Dec-06 14:53 
QuestionThis is a real good contribution !!! Pin
debarun7825-Apr-06 5:40
memberdebarun7825-Apr-06 5:40 
Generalcannot send attachement Pin
hanker19822-Mar-06 22:54
memberhanker19822-Mar-06 22:54 
Generalcannot send email Pin
maxnetpromo6-Dec-05 10:22
membermaxnetpromo6-Dec-05 10:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 15 Nov 2005
Article Copyright 2005 by Sujith John Thomas
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid