Click here to Skip to main content
15,795,793 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I used following code to send mail from pages:

protected void Page_Load(object sender, EventArgs e)
       if (!IsPostBack)
       { }
   protected void Button1_Click(object sender, EventArgs e)
       Response.Write(SendMail("", "", "", "Test Mail", "Test Mail Body"));
    public string SendMail(string toList, string from, string ccList, string subject, string body)
       MailMessage message = new MailMessage();
       SmtpClient smtpClient = new SmtpClient();
       string msg = string.Empty;
           MailAddress fromAddress = new MailAddress(from);
           message.From = fromAddress;
           if (ccList != null && ccList != string.Empty)
           message.Subject = subject;
           message.IsBodyHtml = true;
           message.Body = body;
           smtpClient.Host = "";
           smtpClient.Port = 25;
           smtpClient.UseDefaultCredentials = true;
           smtpClient.Credentials = new System.Net.NetworkCredential("", "XXXXX");

           msg = "Successful";
       catch (Exception ex)

      return msg;

I am getting error such as failure in sending mail at line (smtpClient.Send(message);)

I am trying to send message to my companymail id using
Can any one guide me and help me to come out of this problem?
Updated 22-May-11 11:43am
Wonde Tadesse 18-May-11 12:00pm    
Can you post the exception StackTrace ?
Dalek Dave 22-May-11 16:43pm    
Edited for Grammar and Readability.

If you are working in .NET framework 4.0 and want to send a mail with an attachment size greater that 3 megabyte, then your might need to install a Microsoft .NET framework 4 Client Profile patch.For detail and download, take a look at this [^] KB.


I see. Then here are things to check

1. Firewall is allowing to send mail through the mail server.
2. Network issue.
3. The mail server is properly working.You can check this by sending mail using Microsoft Outlook or Thunderbird[^]

I hope this will help you well.
Share this answer
gowthammanju 18-May-11 8:23am    
i do not need attachements to send ,i need only to send messages to my company mail id from my dot pages
Wonde Tadesse 22-May-11 16:36pm    
Answer updated.
It can be because of various reasons. You need to look at them one by one.

Is the port open? Firewall permissions in place?
Further make sure you have configured SMTP configuration in Web.Config(for ease!) and with valid values:
     <smtp from="">
       <network host="somesmtpserver" port="25" userName="name" password="pass" defaultCredentials="true" />

If needed, have a look at this Microsoft Video tutorial:
Use ASP.NET to send Email from Website[^]
Tutorials on sending Email in ASP.NET[^]
Share this answer
gowthammanju 18-May-11 7:32am    
i too have wrote the same thing but i got error on host name so give me some solutions for finding the smtpservername........

smtpClient.UseDefaultCredentials = false;

Share this answer

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