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

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       { }
   }
   protected void Button1_Click(object sender, EventArgs e)
   {
       Response.Write(SendMail("info@letrouters.com", "info@letrouters.com", "info@letrouters.com", "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;
       try
       {
           MailAddress fromAddress = new MailAddress(from);
           message.From = fromAddress;
           message.To.Add(toList);
           if (ccList != null && ccList != string.Empty)
               message.CC.Add(ccList);
           message.Subject = subject;
           message.IsBodyHtml = true;
           message.Body = body;
           smtpClient.Host = "mail.letrouters.com";
           smtpClient.Port = 25;
           smtpClient.UseDefaultCredentials = true;
           smtpClient.Credentials = new System.Net.NetworkCredential("info@letrouters.com", "XXXXX");

           smtpClient.Send(message);
           msg = "Successful";
       }
       catch (Exception ex)
       {
           Response.Write(ex.Message);
       }

      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 asp.net.
Can any one guide me and help me to come out of this problem?
Posted
Updated 22-May-11 10:43am
v5
Comments
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.

[EDITED]

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
 
v2
Comments
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:
<system.net>
   <mailSettings>
     <smtp from="abc@somedomain.com">
       <network host="somesmtpserver" port="25" userName="name" password="pass" defaultCredentials="true" />
     </smtp>
   </mailSettings>
</system.net>

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
 
Comments
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........
Try:

MIDL
smtpClient.UseDefaultCredentials = false;


See:
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.credentials.aspx[^]
 
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