2014-12-24 2 views
0

goDaddy 메일 서버를 통해 메일을 보내려고합니다. 메일이 전송되는 경우 메일 (나는 내 ​​메일 박스에 수신하고)하지만 catch 블록이 실행하고 쿼리 문자열 MSG와 연락처 페이지에 저를 방문 전송되는 'shouldn,try 블록이 예외를 throw하지 않더라도 catch 블록을 실행합니까?

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"); 
    } 
} 

을 "실패" "contact.html? msg = success"로 리디렉션 하시겠습니까?

+4

시작 디버거와 무슨 일이 일어나고 있는지보고 ...'원을 차단 catch' 예외가없는 경우 실행합니다. – opewix

+5

일어난 일을 로깅하지 않고 예외를 잡는 것은 실제로 나쁜 습관입니다. Ex.Message (적어도) – Steve

+0

예외 세부 사항을 공유하려고하면 Ex.message에서 예외 메시지가 나타납니다. –

답변

2

문제는 첫 번째 Response.Redirect()가 실제로 ThreadAbortException을 던지고 있다는 것입니다. 이것은 .Redirect가 Response.End 메서드를 내부적으로 호출하여이 경우 Response를 조기에 끝내기 때문입니다.

이 작업을 수행하는 올바른 방법은 endResponse를 false로 설정하여 Response.End에 대한 내부 호출을 억제 할 수있는 오버로드 된 Response.Redirect (string url, bool endResponse)를 사용하는 것입니다. 오류가 발생합니다.

는이 모든 MS의 지원 문서에서 설명합니다 : http://support.microsoft.com/kb/312629/EN-US/

그냥 코드를 수정 같이하기 :

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); 
    } 
} 
+0

에게 보내졌습니다. ThreadAbortException을 던져서 문제가 없지만 코드가 더 간단합니다. 다시 한 번 감사드립니다. –

관련 문제