Click here to Skip to main content
Licence 
First Posted 21 Dec 2005
Views 437,710
Downloads 13,105
Bookmarked 82 times

Send Email in ASP.Net 2.0 - Feed back Form

This article explains how to send email using System.Net.Mail.SmtpClient in ASP.Net 2.0. with an example of Feedback form.
10 votes, 17.2%
1
4 votes, 6.9%
2
4 votes, 6.9%
3
8 votes, 13.8%
4
32 votes, 55.2%
5
3.39/5 - 58 votes
μ 3.41, σa 2.76 [?]

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

About the Author

Gowrisankar K

Web Developer

India India

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSending Mail in ASP.Net PinmemberKamal Sukla23:00 14 Nov '11  
Generalnot working Pinmembermahendra07222:09 8 Nov '11  
GeneralMy vote of 1 Pinmemberyograjchaple1:37 3 Sep '11  
Questionsending mail error PinmemberprinceJasper0:58 2 Jul '11  
QuestionError ! Pinmemberrazieh_mohammadi5:30 25 Jun '11  
Generalnice Pinmembersneha_sharma1:00 12 May '11  
GeneralMy vote of 3 Pinmemberali53122:10 8 May '11  
GeneralC# Code for sending email in ASP.NET Pinmembermichaelmeyer23:43 8 Mar '11  
Generalfrom address not reciving..... Pinmembershaillykumar2:26 8 Mar '11  
GeneralMy vote of 5 Pinmembersuryaprabhaa.k1:27 25 Oct '10  
GeneralIt not working for sending e-mail PinmemberMember 24587201:31 19 Sep '10  
GeneralMy vote of 5 Pinmemberanupkum14:06 17 Sep '10  
GeneralMailbox unavailable. Pinmemberchirag.khatsuriya19:28 20 Jun '10  
GeneralRe: Mailbox unavailable. Pinmembersamay_rosy23:37 22 Jul '10  
GeneralRe: Mailbox unavailable. PinmemberMukund Kallapur0:19 19 Sep '10  
GeneralRe: Mailbox unavailable. Pinmembertamannashah199318:59 26 Nov '10  
GeneralMy vote of 1 Pinmemberbarotbhumika6:56 11 Mar '10  
GeneralMy vote of 1 PinmemberMember 447459423:48 7 Mar '10  
GeneralCreating ASP.NET email forms without programming - just drag and drop PinmemberNetexpert4Hire0:18 24 Feb '10  
Generaldemo project isn't working Pinmembermunna moeed21:41 14 Dec '09  
GeneralMy vote of 2 Pinmembereightdang11:12 4 Dec '09  
Generalsend mail Pinmemberminhaj5:46 28 Oct '09  
QuestionPlease help me ur demo mail project not working Pinmembersriharish866:53 19 Oct '09  
GeneralMy vote of 1 PinmemberYouris0:59 22 Sep '09  
Generalplease help me Pinmemberchalamalasetti srinivas23:50 13 Aug '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120206.1 | Last Updated 2 Jan 2006
Article Copyright 2005 by Gowrisankar K
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid