2011-06-18 61 views
-1

如果你可以請幫助我...我嘗試創建o忘記密碼method.access我的數據庫併發送電子郵件。如果你請幫助我,因爲我沒有很多經驗。事先感謝你。登錄是唯一的。 C#sql asp.net發送電子郵件c#

protected void LinkButton1_Click(object sender, EventArgs e) 
{ 

     var sqlCon = new SqlConnection(System.Configuration.ConfigurationManager. 
      ConnectionStrings["ConnectionString"].ConnectionString); 

     SqlCommand cmd = new SqlCommand(); 
     cmd.Connection = sqlCon; 
     cmd.CommandText = "select Password from Person where Login= @Login "; 
     cmd.Connection.Open(); 
     string Password = cmd.ExecuteScalar().ToString(); 
     cmd.Connection.Close(); 
     cmd.Dispose(); 

     SqlCommand cmd1 = new SqlCommand(); 
     cmd1.Connection = sqlCon; 
     cmd1.CommandText = "select e_mail from Person where Login= @Login "; 
     cmd1.Connection.Open(); 
     string e_mail = cmd.ExecuteScalar().ToString(); 
     cmd.Connection.Close(); 
     cmd.Dispose(); 

     if (Login.Text.Equals("@Login")) 
     { 

      Class1 S = new Class1(); 
      S.sendMail(e_mail, "Your Password request", Password); 
      Response.Write("<script>"); 
     } 

} 

回答

1

首先,你只需打一個電話到SQL語句而不是兩個, 嘗試這樣的財產以後(未測試!)

var sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 
SqlCommand cmd = new SqlCommand("select e_mail, Password from Person where Login= @Login ", sqlCon);    
cmd.Connection.Open();    

cmd.Parameters.Add(new SqlParameter("@Login", Email.Text)); 

string email = "", password = ""; 

using (SqlDataReader sdr = cmd.ExecuteReader()) 
{ 
    while (sdr.Read()) 
    { 
     email = sdr.GetString(0); 
     password = sdr.GetString(1); 
    } 
} 
cmd.Connection.Close(); 
cmd.Dispose(); 



SmtpClient mailClient = new SmtpClient("your smtp"); 
MailMessage mailMessage = new MailMessage(); 
mailMessage.From = new MailAddress("YOU"); 
mailMessage.To.Add(email); 

mailMessage.Subject = "password recovery"; 
mailMessage.Body = "hello " + email + " your password : " + password; 
mailMessage.IsBodyHtml = true; 

mailClient.Send(mailMessage); 
+0

再次輸出一個錯誤,{必須聲明標量變量「@Login」} – emilios

+0

現在試試這個,我添加了一個參數.. –

0

要發送郵件使用SmtpClient。在WebConfig中配置客戶端。要使用MailMessage創建電子郵件消息。應該很自我解釋。

我不知道你的用戶數據庫是如何設計的,但有兩個查詢來獲取數據似乎過多。構建一個返回電子郵件和密碼的查詢。

此外,用戶密碼未加密也是不好的做法。考慮提供一種替代解決方案(例如重置密碼而不是返回密碼)。

+0

我有加密我試圖從數據庫訪問它們,這就是問題 – emilios