Click here to Skip to main content
16,020,298 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 705.5K   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

 
GeneralRe: Error Sending Email Pin
Luke J Davis7-Oct-07 22:43
Luke J Davis7-Oct-07 22:43 
QuestionRe: Error Sending Email Pin
kirti118-May-08 20:32
kirti118-May-08 20:32 
QuestionRe: Error Sending Email Pin
kirti118-May-08 20:39
kirti118-May-08 20:39 
QuestionAdd More Text Fields? Pin
chrisplarz13-Sep-07 8:41
chrisplarz13-Sep-07 8:41 
QuestionGetting messege : Failure sending mail.. how to proceed?? Pin
joshiharshu9-Sep-07 17:21
joshiharshu9-Sep-07 17:21 
QuestionSend message to entire contact list Pin
Jay Khanpara28-Jun-07 2:30
Jay Khanpara28-Jun-07 2:30 
GeneralSend email fails Pin
cwhslh26-Jun-07 4:38
cwhslh26-Jun-07 4:38 
Questionhelp with tutorial Pin
AdamGrannell8126-May-07 12:52
AdamGrannell8126-May-07 12:52 
Hi I'm fairly new to ASP.NET and found this useful. I need some help with the aspx.cs file becuase when the page tries to submit the following is shown

'Mailbox unavailable. The server response was: 5.7.1 Unable to relay for adam@adamgrannell.co.uk'

See adamgrannell.co.uk

The source is below.

If some one can check possibly where I have gone wrong and compare it to their version, that would be great. I have tried substituting the 'localhost' with a server IP address but no go.
Everything else is more or less the same as the tutorial.

Initial diagnosis from the website provider say the email is trying to relay through the email address. Must hav done somthing wrong somwhere.

Many thanks

A Grannell
-------------------------------------------------------------------------------------------------
using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;


//This is what the Inherits="_Default" referss to
public partial class _Default : System.Web.UI.Page
{
#region "Send email"
protected void SendEmail_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(txtEmailaddress.Text, txtFullname.Text);

// Have specified the host name or ipaddress of my 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("adam@adamgrannell.co.uk");
message.Subject = "Enquiry from adamgrannell.co.uk website";

// 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("");
//message.CC.Add("");

// You can specify Address directly as string
//message.Bcc.Add(new MailAddress(""));
//message.Bcc.Add(new MailAddress(""));

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

// Message body content- you can show multiple lines
message.Body = txtFullname.Text;
message.Body = WebDesign.Text;
message.Body = WebMaintenance.Text;
message.Body = WebHosting.Text;
message.Body = GraphicDesign.Text;
message.Body = Message.Text;
message.Body = txtPhonenumber.Text;
message.Body = txtEmailaddress.Text;

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

lblStatus.Text = "Your email has been sent successfully.";
}
catch (Exception ex)
{
lblStatus.Text = "Your email could not be sent.
" + ex.Message;
}
}
#endregion

#region "Reset"
protected void Reset_Click(object sender, EventArgs e)
{
txtFullname.Text = "";
WebDesign.Text = "";
WebMaintenance.Text = "";
WebHosting.Text = "";
GraphicDesign.Text = "";
Message.Text = "";
txtPhonenumber.Text = "";
txtEmailaddress.Text = "";
}
#endregion
}
GeneralSender's Confirmation Pin
Matt Lewis15-Mar-07 10:35
Matt Lewis15-Mar-07 10:35 
GeneralPersonalized X-Headers Pin
gr33nman23-Oct-06 4:09
gr33nman23-Oct-06 4:09 
Generalthanks Pin
EXCOM1-Oct-06 13:12
EXCOM1-Oct-06 13:12 
GeneralProblem in Sending mail to specified AMil servers Pin
Exelioindia30-Aug-06 20:52
Exelioindia30-Aug-06 20:52 
GeneralProblem in sending mail Pin
surjit.NET24-Aug-06 0:17
surjit.NET24-Aug-06 0:17 
GeneralRe: Problem in sending mail Pin
darkrd3-Nov-06 17:31
darkrd3-Nov-06 17:31 
QuestionProblem in sending mail.... Pin
surjit.NET23-Aug-06 23:53
surjit.NET23-Aug-06 23:53 
GeneralGood article Pin
Anton H23-Aug-06 23:25
Anton H23-Aug-06 23:25 
GeneralRe: Good article Pin
jiten kumar19-Jun-09 21:09
jiten kumar19-Jun-09 21:09 
GeneralErro de falha no envio... Pin
Gleyson7-Aug-06 3:32
Gleyson7-Aug-06 3:32 
GeneralRe: Erro de falha no envio... Pin
Gowrisankar K21-Aug-06 1:39
Gowrisankar K21-Aug-06 1:39 
Generalhi gowrishakar, i'm muralidhar, Pin
muralidharsp20-Jul-06 2:05
muralidharsp20-Jul-06 2:05 
GeneralGreat Summary Pin
tede barron29-Jun-06 13:55
tede barron29-Jun-06 13:55 
GeneralHello Pin
Ashish_Chotalia21-Jun-06 23:35
Ashish_Chotalia21-Jun-06 23:35 
GeneralRe: Hello Pin
michel petrovic25-Jul-06 21:17
michel petrovic25-Jul-06 21:17 
Generalayana Pin
Nana-j22-Apr-06 16:09
Nana-j22-Apr-06 16:09 
Questionhow do you send the web page Pin
dave19649814-Mar-06 9:25
dave19649814-Mar-06 9:25 

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.