Click here to Skip to main content
15,891,253 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 696.4K   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

 
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 
while i am trying to send an email

ERROR:

Send Email Failed.Mailbox unavailable. The server response was: 5.7.1 Unable to relay for xyz@gmail.com
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 
GeneralUrgent.. Need Solution Pin
vnchandru4-Feb-09 5:16
vnchandru4-Feb-09 5:16 
GeneralRe: Urgent.. Need Solution................... Solution Found Pin
vnchandru4-Feb-09 6:06
vnchandru4-Feb-09 6:06 
GeneralDefault signature for sended Mail Pin
effexoft5-Jan-09 19:00
effexoft5-Jan-09 19:00 
GeneralFailure in Sending Email Pin
jayeshpanicker4-Jan-09 21:54
jayeshpanicker4-Jan-09 21:54 
QuestionModified code is sending wrong emails Pin
Einarolafs14-Dec-08 23:42
Einarolafs14-Dec-08 23:42 
QuestionGot Error 'Mailbox unavailable' [modified] Pin
piyush_patel111114-Dec-08 6:24
piyush_patel111114-Dec-08 6:24 
QuestionHow i can get my smtp host, username and password for sending email in asp.net? Pin
piyush_patel111113-Dec-08 12:26
piyush_patel111113-Dec-08 12:26 
AnswerRe: How i can get my smtp host, username and password for sending email in asp.net? Pin
Mohammed Yahiya11-Feb-09 17:16
Mohammed Yahiya11-Feb-09 17:16 
GeneralFailure sending mail Pin
Latawadhwa15-Sep-08 23:09
Latawadhwa15-Sep-08 23:09 
GeneralSend Email Failed. Pin
Latawadhwa15-Sep-08 19:16
Latawadhwa15-Sep-08 19:16 
Generalsending email from asp.net Pin
swarnameena21-Jul-08 0:59
swarnameena21-Jul-08 0:59 
GeneralEmail Pin
Muhammad Mazhar26-Jun-08 1:00
Muhammad Mazhar26-Jun-08 1:00 

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.