Click here to Skip to main content
15,443,549 members
Articles / Web Development / ASP.NET
Article
Posted 21 Dec 2005

Stats

677.7K views
24.2K downloads
87 bookmarked

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
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

 
QuestionHow to send Mail in RTF format Pin
Sugesh G Pillai28-May-08 10:53
MemberSugesh G Pillai28-May-08 10:53 
AnswerRe: How to send Mail in RTF format Pin
joao.ssa5-Sep-08 8:09
Memberjoao.ssa5-Sep-08 8:09 
Generalhi error :send email failed Pin
surendrabarhate26-May-08 23:43
Membersurendrabarhate26-May-08 23:43 
QuestionRe: hi error :send email failed Pin
Gowrisankar K28-May-08 9:43
MemberGowrisankar K28-May-08 9:43 
Answerfunction toString() { [native code]} Pin
Gowrisankar K4-Jun-08 3:04
MemberGowrisankar K4-Jun-08 3:04 
GeneralSMTP Configuration Pin
Gowrisankar K6-Jun-08 11:43
MemberGowrisankar K6-Jun-08 11:43 
Generalgetting failed msg, always Pin
sankar98-May-08 0:25
Membersankar98-May-08 0:25 
GeneralSimple and easy to understand Pin
gujjeli22-Apr-08 23:33
Membergujjeli22-Apr-08 23:33 
The article is simle and very easy to uderstand.
It is benificial to beginners Smile | :)
GeneralSend Email Failed Pin
Member 42510287-Apr-08 1:33
MemberMember 42510287-Apr-08 1:33 
GeneralHi plz help me if anyone has any answer.. i m getting Send Email Failed Error Pin
suwarn27-Mar-08 23:03
Membersuwarn27-Mar-08 23:03 
GeneralRe: Hi plz help me if anyone has any answer.. i m getting Send Email Failed Error Pin
Navis Singarayan27-Aug-08 1:33
MemberNavis Singarayan27-Aug-08 1:33 
Questionprevent to be treated as spam mail.is there any option? Pin
BeBadgujar5-Mar-08 1:59
MemberBeBadgujar5-Mar-08 1:59 
Generalhai pls help me. error in my code. Pin
rtsviswa30-Nov-07 0:05
Memberrtsviswa30-Nov-07 0:05 
GeneralRe: hai pls help me. error in my code. Pin
Navis Singarayan27-Aug-08 1:35
MemberNavis Singarayan27-Aug-08 1:35 
GeneralGreat Article Pin
adillhoff17-Nov-07 14:10
Memberadillhoff17-Nov-07 14:10 
GeneralError Sending Email Pin
jaydee77714-Sep-07 0:43
Memberjaydee77714-Sep-07 0:43 
GeneralRe: Error Sending Email Pin
Luke J Davis7-Oct-07 22:43
MemberLuke J Davis7-Oct-07 22:43 
QuestionRe: Error Sending Email Pin
kirti118-May-08 20:32
Memberkirti118-May-08 20:32 
QuestionRe: Error Sending Email Pin
kirti118-May-08 20:39
Memberkirti118-May-08 20:39 
QuestionAdd More Text Fields? Pin
chrisplarz13-Sep-07 8:41
Memberchrisplarz13-Sep-07 8:41 
QuestionGetting messege : Failure sending mail.. how to proceed?? Pin
joshiharshu9-Sep-07 17:21
Memberjoshiharshu9-Sep-07 17:21 
QuestionSend message to entire contact list Pin
Jay Khanpara28-Jun-07 2:30
MemberJay Khanpara28-Jun-07 2:30 
GeneralSend email fails Pin
cwhslh26-Jun-07 4:38
Membercwhslh26-Jun-07 4:38 
Questionhelp with tutorial Pin
AdamGrannell8126-May-07 12:52
MemberAdamGrannell8126-May-07 12:52 
GeneralSender's Confirmation Pin
Matt Lewis15-Mar-07 10:35
MemberMatt Lewis15-Mar-07 10:35 

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.