tags:

views:

267

answers:

2

in my forgot password page user enters email id and clicks on submit button,the submit click event sends a mail on his emailid,,now i m getting an error that smtp server requirs a secure connection or the client was not authenticated,,let me show my code

 protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
    {
        if (Page.IsValid)
        {
            PasswordRecovery1.MailDefinition.From = "[email protected]";

            e.Message.IsBodyHtml = false;
            e.Message.Subject = "Please read below to reset your password.";

            e.Message.Body = e.Message.Body.Replace("<%email%>", PasswordRecovery1.UserName);

            SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["DSN"]);
            SqlCommand userid = new SqlCommand("SELECT UserId from aspnet_Users WHERE UserName=@UserName", connection);
            connection.Open();
            userid.Parameters.Add("@UserName", SqlDbType.VarChar, 50);
            userid.Parameters["@UserName"].Value = PasswordRecovery1.UserName;
            SqlDataReader result = userid.ExecuteReader();
            string UserId = "";
            while (result.Read())
            {
                object obValue = result.GetValue(0);
                UserId = obValue.ToString();
            }
            connection.Close();
            string link = "http://www.fixpic.com/Passwordreset.aspx?userid=" + UserId;
            e.Message.Body = e.Message.Body.Replace("<%resetlink%>", link);

            SmtpClient smtpClient = new SmtpClient();

            smtpClient.EnableSsl = true;
            smtpClient.Send(e.Message);

            e.Cancel = true;

        }
    }

in web.config i have defined the mail settings as

<mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]">
        <network host="smtp.gmail.com" port="587" defaultCredentials="false" />
      </smtp>
    </mailSettings>
+2  A: 

You need to be authenticated with GMail's SMTP:

var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("[email protected]", "secret");

var mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
Darin Dimitrov