title: Failure sending mail using asp.net
body: <p>Below is my code for sending confirmation link to mail and I am getting errors like Failure sending mail. </p>
  
  <p>After sending confirmation link to email then user will reset the password.</p>
  
  <pre><code> 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 Latin1_general_CS_AS=@username or EmailId COLLATE Latin1_general_CS_AS=@emailId", 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 Uniquecode=@uniqueCode where UserName=@username or EmailId=@emailid", 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("&lt;a href=http://localhost:2464/SampleApplication/ResetPassword.aspx?emailId=" + 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@gmail.com", dr["EmailId"].ToString(), "Reset Your Password", strBody.ToString());
              //pasing the Gmail credentials to send the email
  
              System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential("SenderEmailIAddress@gmail.com", "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();
      }
  </code></pre>
  
  <p>Web. con file code given below and don't know its correct.</p>
  
  <pre><code>   &lt;system.net&gt;
       &lt;mailSettings&gt;
       &lt;smtp from="abc@somedomain.com"&gt;
        &lt;network host="somesmtpserver" port="25" userName="name"         
                                   password="pass" defaultCredentials="true" /&gt;
    &lt;/smtp&gt;
  &lt;/mailSettings&gt;
  </code></pre>
  
  <p></p>