title: Failure sending message in smtp/The SMTP server requires a secure connection or the client was not authenticated
body: <p>I'm trying to do a reset password and recover password function. Therefore i used the smtp way to send the mail. However, i got an error from my try catch.</p>
  
  <pre><code>Error Occured: Failure sending mail.
  </code></pre>
  
  <p>I'm also not sure which part of my web.config file should i add this connection code</p>
  
  <pre><code>&lt;network host="smtp.gmail.com" enableSsl="true"  /&gt;
  </code></pre>
  
  <p>Below is my connection to my Azure database while having the smtp logic.</p>
  
  <pre><code>protected void btnSubmit_Click(object sender, EventArgs e)
      {
          string uniqueCode = string.Empty;
          SqlCommand cmd = new SqlCommand();
          SqlDataReader dr;
          try
          {
              SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
              if (con.State == ConnectionState.Closed)
              {
                  con.Open();
              }
              // get the records matching the supplied username or email id.         
              cmd = new SqlCommand("select * from MemberAccount where nric COLLATE Latin1_general_CS_AS=@nric or email COLLATE Latin1_general_CS_AS=@email", con);
  
              cmd.Parameters.AddWithValue("@nric", Convert.ToString(txtUserName.Text.Trim()));
              cmd.Parameters.AddWithValue("@email", 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 MemberAccount set UniqueCode=@uniqueCode where nric=@nric or email=@email", con);
                  cmd.Parameters.AddWithValue("@uniqueCode", uniqueCode);
                  cmd.Parameters.AddWithValue("@nric", txtUserName.Text.Trim());
                  cmd.Parameters.AddWithValue("@email", 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("&lt;a href=http://sipolice.azurewebsites.net/MemberRecoverPassword.aspx" + txtEmailId.Text + "&amp;uName=" + txtUserName.Text + "&amp;uCode=" + uniqueCode + "&gt;Click here to change your password&lt;/a&gt;");
                  // sbody.Append("&amp;uCode=" + uniqueCode + "&amp;uName=" + txtUserName.Text + "&gt;Click here to change your password&lt;/a&gt;");
  
                  System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("SenderEmailIAddress@hotmail.com", dr["email"].ToString(), "Reset Your Password", strBody.ToString());
                  //pasing the Gmail credentials to send the email
                  System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential("SenderEmailIAddress@hotmail.com", "SenderPassword");
  
                  System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient("smtp.hotmail.com", 587);
  
                  mailclient.EnableSsl = true;
                  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();
          }
      }
  </code></pre>
  
  <p><em><strong>UPDATE</em></strong>
  I just realized why it couldn't work. It did not work as my account information is incorrect</p>