Click here to Skip to main content
15,846,211 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System.Net.Mail
#region Email
    protected void btnSend_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();

            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.Subject = "Feedback";

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("")

            // 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 = false;

            // Message body content
            message.Body = txtComment.Text;

            // Send SMTP mail

            lblStatus.Text = "Email successfully sent.";
        catch (Exception ex)
            lblStatus.Text = "Send Email Failed.<br>" + ex.Message;

Error: Sending email failed
Updated 16-Aug-11 20:55pm

Make sure you are able to connect to the outlook server.
The credentials need to be valid as well.
Share this answer
Manfred Rudolf Bihy 17-Aug-11 2:21am    
What is an outlook server? Are you talking about an Exchange Server maybe?
Abhinav S 17-Aug-11 3:51am    
Whoops sorry - I meant to write Outlook Exchange Server.
Since you have output the message into a label and decided to insert a line break <br> you might not be seeing the interesting part of the error message. That is the part added after the line break by ex.Message.

I'll make an attempt at producing some Liquid Nitrogen[^] and throw you some educated guesses:

  1. If there is no SMTP server running on the machine hosting your website the SmptClient will not be able to connect to the SMPT server on localhost (
  2. There may be a SMTP server running on your machine, but it's not listening to the well known SMTP port 25, but rather some other more esoteric port number.
  3. The SMTP server on your machine requires the sender of an email to authenticate themselves. So you'd have to send some NetworkCredentials along with your request.

To make sure that there is a SMTP server running on your server open up a command prompt window and do this:
c:\ telnet localhost 25 followed by enter.
If you have a SMPT server listening on port 25 it will connect and you can start issueing SMTP commands from the prompt to further debug your issue. The SMTP protocol is very simplistic and you can easily google a source for yourself.
If you can't connect there either is no SMPT server on your machine or it listens to a different port. This should be an issue of the administrator of that server, so kindly talk to that person and ask for help.

Best Regards,

Share this answer
Your code is totally messed up, to send email use this below way

MailMessage message = new MailMessage();

message.From = new MailAddress("");


message.To.Add(new MailAddress(""));

message.To.Add(new MailAddress(""));

message.To.Add(new MailAddress(""));


message.CC.Add(new MailAddress(""));

message.Subject = "This is my subject";

message.Body = "This is the content";


SmtpClient client = new SmtpClient();


System.Net.Mail reads SMTP configuration data out of the standard .NET configuration system (so for ASP.NET applications you’d configure this in your application’s web.config file). Here is an example of how to configure it:



  <smtp from="">

    <network host="smtpserver1" port="25" username="username" password="secret" defaultcredentials="true" />




Hope this helps,
Share this answer
Manfred Rudolf Bihy 17-Aug-11 2:22am    
Would you care to properly format your code? Hint: A check box needs to be unchecked. Use the link "Improve solution" edit your solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900