Click here to Skip to main content
15,886,740 members
Articles / Web Development / ASP.NET
Article

Send Email in ASP.Net 2.0 - Feed back Form

Rate me:
Please Sign up or sign in to vote.
3.53/5 (65 votes)
1 Jan 2006 694.7K   24.2K   87   118
This article explains how to send email using System.Net.Mail.SmtpClient in ASP.Net 2.0. with an example of Feedback form.

Sample Image - EmailApplication.jpeg

Introduction

We are using System.Web.Mail.SmtpMail to send email in dotnet 1.1 which is obsolete in 2.0. The System.Net.Mail.SmtpClient Class will provide us the same feature as that of its predecessor.

This article explains how to use System.Net.Mail namespace to send emails.

Using the code

The HTML Design contains provision to enter sender’s name, email id and his comments. On click of the send email button the details will be sent to the specified email (Admin).

The Send mail functionality is similar to Dotnet 1.1 except for few changes

  1. System.Net.Mail.SmtpClient is used instead of System.Web.Mail.SmtpMail (obsolete in Dotnet 2.0).
  2. System.Net.MailMessage Class is used instead of System.Web.Mail.MailMessage (obsolete in Dotnet 2.0)
  3. The System.Net.MailMessage class collects From address as MailAddress object.
  4. The System.Net.MailMessage class collects To, CC, Bcc addresses as MailAddressCollection.
  5. MailMessage Body Format is replaced by IsBodyHtml

The Code is Self explanatory by itself.

protected void btnSendmail_Click(object sender, EventArgs e)
{
  // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
  // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
  SmtpClient smtpClient = new SmtpClient();
  MailMessage message = new MailMessage();

  try
  {
      MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);

      // You can specify the host name or ipaddress of your server
      // Default in IIS will be localhost
      smtpClient.Host = "localhost";

      //Default port will be 25
      smtpClient.Port = 25;

      //From address will be given as a MailAddress Object
      message.From = fromAddress;

      // To address collection of MailAddress
      message.To.Add("admin1@yoursite.com");
      message.Subject = "Feedback";

      // CC and BCC optional
      // MailAddressCollection class is used to send the email to various users
      // You can specify Address as new MailAddress("admin1@yoursite.com")
      message.CC.Add("admin1@yoursite.com");
      message.CC.Add("admin2@yoursite.com");

      // You can specify Address directly as string
      message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
      message.Bcc.Add(new MailAddress("admin4@yoursite.com"));

      //Body can be Html or text format
      //Specify true if it  is html message
      message.IsBodyHtml = false;

      // Message body content
      message.Body = txtMessage.Text;

      // Send SMTP mail
      smtpClient.Send(message);

      lblStatus.Text = "Email successfully sent.";
  }
  catch (Exception ex)
  {
      lblStatus.Text = "Send Email Failed." + ex.Message;
  }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalfrom address not reciving..... Pin
shaillykumar8-Mar-11 1:26
shaillykumar8-Mar-11 1:26 
GeneralMy vote of 5 Pin
suryaprabhaa.k25-Oct-10 0:27
suryaprabhaa.k25-Oct-10 0:27 
GeneralIt not working for sending e-mail Pin
Member 245872019-Sep-10 0:31
Member 245872019-Sep-10 0:31 
GeneralMy vote of 5 Pin
anupkum17-Sep-10 13:06
anupkum17-Sep-10 13:06 
GeneralMailbox unavailable. Pin
chirag.khatsuriya20-Jun-10 18:28
chirag.khatsuriya20-Jun-10 18:28 
GeneralRe: Mailbox unavailable. Pin
samay_rosy22-Jul-10 22:37
samay_rosy22-Jul-10 22:37 
GeneralRe: Mailbox unavailable. Pin
Mukund Kallapur18-Sep-10 23:19
Mukund Kallapur18-Sep-10 23:19 
GeneralRe: Mailbox unavailable. Pin
tamannashah199326-Nov-10 17:59
tamannashah199326-Nov-10 17:59 
GeneralRe: Mailbox unavailable. Pin
BALAKUMARAN198719-Jul-12 21:03
BALAKUMARAN198719-Jul-12 21:03 
GeneralMy vote of 1 Pin
barotbhumika11-Mar-10 5:56
barotbhumika11-Mar-10 5:56 
GeneralMy vote of 1 Pin
Member 44745947-Mar-10 22:48
Member 44745947-Mar-10 22:48 
GeneralCreating ASP.NET email forms without programming - just drag and drop Pin
Netexpert4Hire23-Feb-10 23:18
Netexpert4Hire23-Feb-10 23:18 
Generaldemo project isn't working Pin
munna moeed14-Dec-09 20:41
munna moeed14-Dec-09 20:41 
GeneralMy vote of 2 Pin
eightdang4-Dec-09 10:12
eightdang4-Dec-09 10:12 
Generalsend mail Pin
minhaj28-Oct-09 4:46
minhaj28-Oct-09 4:46 
QuestionPlease help me ur demo mail project not working Pin
sriharish8619-Oct-09 5:53
sriharish8619-Oct-09 5:53 
GeneralMy vote of 1 Pin
Youris21-Sep-09 23:59
Youris21-Sep-09 23:59 
Generalplease help me Pin
chalamalasetti srinivas13-Aug-09 22:50
chalamalasetti srinivas13-Aug-09 22:50 
Questiondoubt, regarding Feedback form Pin
swathibhouni21-Jul-09 0:45
swathibhouni21-Jul-09 0:45 
Questioncan u help me ? Pin
swati saxena200824-Apr-09 20:46
swati saxena200824-Apr-09 20:46 
Generalmail sent...but not in my mailbox.. Pin
kishore machineni18-Apr-09 20:04
kishore machineni18-Apr-09 20:04 
GeneralMy vote of 1 Pin
anju310115-Apr-09 5:40
anju310115-Apr-09 5:40 
GeneralI didnt received any mail message in my email address when i click submit button . so guide me Pin
muralishift10-Apr-09 22:59
muralishift10-Apr-09 22:59 
GeneralRe: I didnt received any mail message in my email address when i click submit button . so guide me Pin
MariaMadalina15-Apr-09 9:47
MariaMadalina15-Apr-09 9:47 
GeneralRe: I didnt received any mail message in my email address when i click submit button . so guide me Pin
hishamst24-Aug-09 9:11
hishamst24-Aug-09 9:11 

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.