2012-09-27 61 views
-1
public partial class ForgotPassword : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void btnPass_Click(object sender, EventArgs e) 
    { 
     //Create Connection String And SQL Statement 
     string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
     string strSelect = "SELECT UserName,Password FROM Users WHERE Email = @Email"; 

     SqlConnection connection = new SqlConnection(strConnection); 
     SqlCommand command = new SqlCommand(); 
     command.Connection = connection; 
     command.CommandType = CommandType.Text; 
     command.CommandText = strSelect; 

     SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50); 
     email.Value = txtEmail.Text.Trim().ToString(); 
     command.Parameters.Add(email); 

     //Create Dataset to store results and DataAdapter to fill Dataset 
     DataSet dsPwd = new DataSet(); 
     SqlDataAdapter dAdapter = new SqlDataAdapter(command); 
     connection.Open(); 
     dAdapter.Fill(dsPwd); 
     connection.Close(); 
     if(dsPwd.Tables[0].Rows.Count > 0) 
     { 

      MailMessage loginInfo = new MailMessage(); 
      loginInfo.To.Add(txtEmail.Text.ToString()); 
      loginInfo.From = new MailAddress("[email protected]"); 
      loginInfo.Subject = "Forgot Password Information"; 

      loginInfo.Body = "Username: " + dsPwd.Tables[0].Rows[0]["UserName"] + "<br /><br />Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "<br /><br />"; 
      loginInfo.IsBodyHtml = true; 
      SmtpClient smtp = new SmtpClient(); 
      smtp.Host = "smtp.gmail.com"; 
      smtp.Port = 587; 
      smtp.EnableSsl = true; 
      smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "YourGmailPassword"); 
      smtp.Send(loginInfo); 
      lblMessage.Text = "Password is sent to you email id,you can now <a href='Login.aspx'>Login</a>"; 
     } 
     else 
     { 
      lblMessage.Text = "Email Address Not Registered"; 
     } 

    } 
} 

我遇到了錯誤smtpException是由用戶代碼未處理...如何解決它?如何解決smtpException被用戶代碼未處理

+1

未處理在哪一行沒有異常發生的? – Tudor

+0

smtp.Send(loginInfo); –

回答

2

很簡單,寫一些處理它的用戶代碼!

如果您不熟悉在try..catch塊,我建議你做一些閱讀它們,並使用它們(如適用)來處理特殊的情況下(即郵件服務器離線,網絡連接問題,密碼過期)。

的代碼導致您的例外是最有可能這條線,這應該給你的地方開始:

smtp.Send(loginInfo); 
+0

只需在這裏添加一般建議,'SqlConnection' /'SqlCommand'和'SqlDataAdapter'應該封裝在[using語句](http://msdn.microsoft.com/zh-cn/library/yh598w02(v = VS.80)的.aspx)。 – James

0

您應該處理用戶代碼中的異常:

try 
{ 
    smtp.Send(loginInfo); 
} 
catch (Exception ex) 
{ 
    //Handle your exception here 
    lblMessage.Text = "Oeps, something when wrong when we tried to send the email"; 
    return; 
} 
+0

當然,你應該在這裏捕獲相關類型的異常並且適當地處理它...... – Jamie

0

的問題是你的使用錯誤的端口號。適用於SSL的Gmail。從documentation

Outgoing Mail (SMTP) Server - requires TLS3 or SSL: smtp.gmail.com (use authentication) 
Use Authentication: Yes 
Port for TLS/STARTTLS: 587 
Port for SSL: 465 

正如你所EnableSsl選項設置爲true,你的端口號應設置爲465

-1

錯誤:郵件不能發送 - SMTP的例外是由用戶代碼

if (ModelState.IsValid) 
     { 
      try{ 
      string from = "[email protected]"; 
      using (MailMessage mail = new MailMessage(from, obj.Idemail)) 
      { 
       mail.Subject = "Admin"; 
       mail.Body = "Registration successful!"; 
       mail.IsBodyHtml = false; 
       SmtpClient smtp = new SmtpClient(); 
       smtp.Host = "smtp.gmail.com"; 
       smtp.EnableSsl = true; 
       NetworkCredential networkCredential = new NetworkCredential(from, "pswd"); 
       smtp.UseDefaultCredentials = true; 
       smtp.Credentials = networkCredential; 
       smtp.Port = 587; 
       smtp.Host = "localhost"; 
       smtp.Send(mail); 
       ViewBag.Message = "Password Sent through mail. Please chack password in your account and signup."; 
      } 
      } 
      catch (Exception ex) 
      { 
       ViewBag.Message = "Error occur for sending data"; 
      } 
     } 

     else 
     { 
      return Json("Invalid Information!", JsonRequestBehavior.AllowGet); 
     } 
+1

雖然你在將代碼塊包裝在try-catch中來處理拋出的異常是正確的,但我認爲在這種情況下,它不會提供與外部因素(如配置或網絡)有關的問題的建設性解決方案。 –

相關問題