2015-05-01 143 views
0

下面是我發送確認鏈接到郵件的代碼,我收到錯誤如發送郵件失敗。使用asp.net發送郵件失敗

將確認鏈接發送至電子郵件後,用戶將重置密碼。

string uniqueCode = string.Empty; 
    SqlCommand cmd = new SqlCommand(); 
    SqlDataReader dr; 
    try 
    { 
     SqlConnection con = new  SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString); 
     if (con.State == ConnectionState.Closed) 
     { 
      con.Open(); 
     } 
     // get the records matching the supplied username or email id.   
     cmd = new SqlCommand("select * from Tbl_Login where UserName COLLATE [email protected] or EmailId COLLATE [email protected]", con); 

     cmd.Parameters.AddWithValue("@username", Convert.ToString(txtUserName.Text.Trim())); 
     cmd.Parameters.AddWithValue("@emailId", Convert.ToString(txtEmailId.Text.Trim())); 
     dr = cmd.ExecuteReader(); 
     cmd.Dispose(); 
     if (dr.HasRows) 
     { 
      dr.Read(); 
      //generate unique code 
      uniqueCode = Convert.ToString(System.Guid.NewGuid()); 
      //Updating an unique random code in then UniquCode field of the database table 
      cmd = new SqlCommand("update Tbl_Login set [email protected] where [email protected] or [email protected]", con); 
      cmd.Parameters.AddWithValue("@uniqueCode", uniqueCode); 
      cmd.Parameters.AddWithValue("@username", txtUserName.Text.Trim()); 
      cmd.Parameters.AddWithValue("@emailid", txtEmailId.Text.Trim()); 

      StringBuilder strBody = new StringBuilder(); 
      //Passing emailid,username and generated unique code via querystring. For testing pass your localhost number and while making online pass your domain name instead of localhost path. 
      strBody.Append("<a href=http://localhost:2464/SampleApplication/ResetPassword.aspx?emailId=" + txtEmailId.Text + "&uName=" + txtUserName.Text + "&uCode=" + uniqueCode + ">Click here to change your password</a>"); 
      // sbody.Append("&uCode=" + uniqueCode + "&uName=" + txtUserName.Text + ">Click here to change your password</a>"); 

      System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("[email protected]", dr["EmailId"].ToString(), "Reset Your Password", strBody.ToString()); 
      //pasing the Gmail credentials to send the email 

      System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential("[email protected]", "SenderPassword"); 

      System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587); 
      mailclient.EnableSsl = true; 
      mailclient.UseDefaultCredentials = false; 
      mailclient.Credentials = mailAuthenticaion; 
      mail.IsBodyHtml = true; 
      mailclient.Send(mail); 
      dr.Close(); 
      dr.Dispose(); 
      cmd.ExecuteReader(); 
      cmd.Dispose(); 
      con.Close(); 
      lblStatus.Text = "Reset password link has been sent to your email address"; 
      txtEmailId.Text = string.Empty; 
      txtUserName.Text = string.Empty; 
     } 
     else 
     { 
      lblStatus.Text = "Please enter valid email address or username"; 
      txtEmailId.Text = string.Empty; 
      txtUserName.Text = string.Empty; 
      con.Close(); 
      return; 
     } 
    } 
    catch (Exception ex) 
    { 
     lblStatus.Text = "Error Occured: " + ex.Message.ToString(); 
    } 
    finally 
    { 
     cmd.Dispose(); 
    } 

Web。 con文件代碼如下,不知道它的正確性。

<system.net> 
    <mailSettings> 
    <smtp from="[email protected]"> 
     <network host="somesmtpserver" port="25" userName="name"   
           password="pass" defaultCredentials="true" /> 
    </smtp> 
</mailSettings> 

+0

你得到哪些錯誤?您是否嘗試降低gmail帳戶設置的安全性? – FeliceM

+0

無法發送郵件錯誤,我不知道發生了什麼,其防病毒或Gmail帳戶設置的問題不知道該怎麼辦。 – mohdmazharkhan

+0

發送郵件失敗。 @FeliceM – mohdmazharkhan

回答

0
MailMessage mail = new MailMessage(); 
mail.To.Add([email protected]); 
mail.From = new MailAddress("[email protected]"); 
mail.CC.Add("[email protected]"); 
mail.Subject = "Write Subject Here"; 
string body = "Write your body Here"; 

mail.Body = body; 

mail.IsBodyHtml = true; 
SmtpClient smtp = new SmtpClient("[email protected]"); 
mail.Headers.Add("Disposition-Notification-To", "[email protected]"); 
smtp.Host = "XXX.XXX.XX.XX"; //Or Your SMTP Server Address 
smtp.Port = 25; 

//Or your Smtp Email ID and Password 
smtp.EnableSsl = false; 
smtp.Send(mail); 

****If you are using Gmail account-**** 

smtp.Port = 587; 
smtp.EnableSsl = true;