0

I am trying to send a mail via goDaddy mail server. The mail is being sent (I am receiving it in my mailbox) but the catch block is executing and landing me to the contact page with querystring msg as "fail"

 protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["username"] != null && Request.QueryString["email"] != null && Request.QueryString["msg"] != null) { try { string name = Request.QueryString[0]; string email = Request.QueryString[1]; string msg = Request.QueryString[2]; SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net", 25); //smtp.EnableSsl = false; smtp.Send("[email protected]", "[email protected]", "feedback", "Name:" + name + "\n e-mail:" + email + "\n Subject:" + msg); Response.Redirect("contact.html?msg=success"); } catch(Exception ex) { Response.Redirect("contact.html?msg=fail"); } } else { Response.Redirect("contact.html"); } } 

If the mail is being sent, shouldn't it redirect to "contact.html?msg=success"?

4
  • 4
    Start debugger and watch what is going on... catch block won't execute if there is no exception Commented Dec 24, 2014 at 12:33
  • 5
    Catching an exception without logging what has happened is really a bad practice. Try to save somewhere the ex.Message (at least) Commented Dec 24, 2014 at 12:34
  • share exception detail, get the exception message from ex.message Commented Dec 24, 2014 at 12:34
  • exception thrown is "Thread was being aborted". But the mail was sent Commented Dec 24, 2014 at 12:45

1 Answer 1

2

The problem is that your first Response.Redirect() is actually throwing a ThreadAbortException. This is due to the fact that .Redirect internally calls the Response.End method, prematurely ending the Response in this case.

The correct way to do this is to use the overloaded Response.Redirect(string url, bool endResponse), which allows you to suppress the internal call to Response.End by setting endResponse to false ... which will prevent this error from being thrown.

This is all covered in this MS support article: http://support.microsoft.com/kb/312629/EN-US/

Just modify your code to look like this:

protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["username"] != null && Request.QueryString["email"] != null && Request.QueryString["msg"] != null) { try { string name = Request.QueryString[0]; string email = Request.QueryString[1]; string msg = Request.QueryString[2]; SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net", 25); //smtp.EnableSsl = false; smtp.Send("[email protected]", "[email protected]", "feedback", "Name:" + name + "\n e-mail:" + email + "\n Subject:" + msg); Response.Redirect("contact.html?msg=success", false); } catch(Exception ex) { Response.Redirect("contact.html?msg=fail", false); } } else { Response.Redirect("contact.html", false); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I got it alright by throwing the ThreadAbortException, but your code is more simple. thanks again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.