0

I am building a hotel site for my final year project, I am trying to send an automated email to the customers once they have book.

used this code:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(email.Text);
message.Subject = "Hotel Reservation";
message.From = new System.Net.Mail.MailAddress("Reservation@hotel.com");
message.Body = ("Dear" + first_name.Text + "Your reservation made for " + rates.Text +" " + nor.Text + " " + "is successful. Please print out this email for reference while checkin. Thank you");
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost"); smtp.Send(message);

when i run it shows me this error highlighting smtp.Send(message);:

SmtpException was unhandled by the user code and Failure sending mail.

user1617943
  • 11
  • 1
  • 6
  • That's not a lot to go on. Perhaps you could add code that handles the `SmtpException`, as the error message suggests? – Robert Harvey Aug 22 '12 at 19:35
  • Thanks for ur reply.. :) Actually i thought of it.. and i added `using System.Net.Mail.SmtpException;`.. It still gave me an error then i added `smtp.EnableSsl = true;` it still gave me error.. i dont really know how about to do it.. I tried searching online couldnt really find the sample code.. :( it will be really kind of you if you could help me out.. Please.. :| – user1617943 Aug 22 '12 at 19:42
  • 1
    Trap the exception, and examine the inner exception (if there is one). That may tell you more about what is going wrong. You should write mail sending code with the expectation that mail sends will fail from time to time, so handling that situation should be included in your code. – hatchet - done with SOverflow Aug 22 '12 at 19:45
  • This may be helpful http://stackoverflow.com/questions/372742/can-i-test-smtpclient-before-calling-client-send (especially the second answer). – hatchet - done with SOverflow Aug 22 '12 at 19:58

1 Answers1

0

There are several reasons for this error.

  1. To Email is invalid
  2. You have not specified credentials. (UserName / Password)
  3. There might be a firewall problem
  4. SMTP port might be other than 25
Ibrahim ULUDAG
  • 450
  • 3
  • 9