Click here to Skip to main content
6,595,854 members and growing! (18,543 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Send Email in ASP.Net 2.0 - Feed back Form

By Gowrisankar K

This article explains how to send email using System.Net.Mail.SmtpClient in ASP.Net 2.0. with an example of Feedback form.
Windows, .NET, ASP.NET, Visual Studio, Dev
Posted:21 Dec 2005
Updated:1 Jan 2006
Views:273,492
Bookmarked:66 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
45 votes for this article.
Popularity: 5.69 Rating: 3.44 out of 5
7 votes, 15.6%
1
3 votes, 6.7%
2
3 votes, 6.7%
3
6 votes, 13.3%
4
26 votes, 57.8%
5

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


Member

Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 79 (Total in Forum: 79) (Refresh)FirstPrevNext
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  
Questiondoubt, regarding Feedback form Pingroupswathibhouni1:45 21 Jul '09  
Generalcan u help me ? Pinmemberswati saxena200821:46 24 Apr '09  
Generalmail sent...but not in my mailbox.. Pinmemberkishore machineni21:04 18 Apr '09  
GeneralMy vote of 1 Pinmemberanju31016:40 15 Apr '09  
GeneralI didnt received any mail message in my email address when i click submit button . so guide me Pinmembermuralishift23:59 10 Apr '09  
GeneralRe: I didnt received any mail message in my email address when i click submit button . so guide me PinmemberMariaMadalina10:47 15 Apr '09  
GeneralRe: I didnt received any mail message in my email address when i click submit button . so guide me Pinmemberhishamst10:11 24 Aug '09  
GeneralUrgent.. Need Solution Pinmembervnchandru6:16 4 Feb '09  
GeneralRe: Urgent.. Need Solution................... Solution Found Pinmembervnchandru7:06 4 Feb '09  
GeneralDefault signature for sended Mail Pinmembereffexoft20:00 5 Jan '09  
GeneralFailure in Sending Email Pinmemberjayeshpanicker22:54 4 Jan '09  
QuestionModified code is sending wrong emails PinmemberEinarolafs0:42 15 Dec '08  
QuestionGot Error 'Mailbox unavailable' [modified] Pinmemberpiyush_patel11117:24 14 Dec '08  
QuestionHow i can get my smtp host, username and password for sending email in asp.net? Pinmemberpiyush_patel111113:26 13 Dec '08  
AnswerRe: How i can get my smtp host, username and password for sending email in asp.net? PinmemberMohammed Yahiya18:16 11 Feb '09  
GeneralFailure sending mail PinmemberLatawadhwa0:09 16 Sep '08  
GeneralSend Email Failed. PinmemberLatawadhwa20:16 15 Sep '08  
Generalsending email from asp.net Pinmemberswarnameena1:59 21 Jul '08  
GeneralEmail PinmemberMuhammad Mazhar2:00 26 Jun '08  
Generalhi Pinmembersurendrabarhate22:16 28 May '08  
Questionhi shanker error is Pinmembersurendrabarhate20:48 28 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Jan 2006
Editor:
Copyright 2005 by Gowrisankar K
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project