Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a form that I drag and dropped the PasswordRecovery control to the form. When a user forgets their password, how can I setup the control to send the password from the database that matches the user email?

C#
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Net.Mail;

public partial class ForgotPassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnPass_Click(object sender, EventArgs e)
    {
        //Create Connection String And SQL Statement
        string strConnection = ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString;
        string strSelect = "SELECT EmailAddress, Password FROM TableSecurity WHERE EmailAddress = @EmailAddress";

        SqlConnection connection = new SqlConnection(strConnection);
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.Text;
        command.CommandText = strSelect;

        SqlParameter email = new SqlParameter("@EmailAddress", SqlDbType.VarChar, 50);
        email.Value = TextBoxEA.Text.Trim().ToString();
        command.Parameters.Add(email);

        //Create Dataset to store results and DataAdapter to fill Dataset 
        DataSet dsPwd = new DataSet();
        connection.Open();
        SqlDataAdapter dAdapter = new SqlDataAdapter(command);
        dAdapter.Fill(dsPwd);
        connection.Close();
        if (dsPwd.Tables[0].Rows.Count < 0)
        {
            MailMessage loginInfo = new MailMessage();
            loginInfo.To.Add(TextBoxEA.Text.ToString());
            loginInfo.From = new MailAddress("YourID@sasccoc.org");
            loginInfo.Subject = "Forgot Password Information";

            loginInfo.Body = "EmailAddress: " + dsPwd.Tables[0].Rows[0]["EmailAddress"] + "<br /><br />Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "<br /><br />";
            loginInfo.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "mail.sasccoc.org";
            smtp.Port = 25;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("YourEmailID@sasccoc.org", "YourEmailPassword");
            smtp.Send(loginInfo);
            lblMessage.Text = "Password is sent to you email id,you can now <a href='Login.aspx'>Login</a>";

            try
            {
                smtp.Send(loginInfo);
            }
            catch (Exception ex)
            {

                lblMessage.Text = "Oops, Something Went Wrong When We Tried to Send The Email";
                return;
            }

        }
        else
        {
            lblMessage.Text = "Email Address Not Registered";
        }

    }
}
Posted
Updated 15-Nov-13 4:29am
v3

1 solution

First retrive the email id and password of user to whom you want to send password, and then

MailMessage newMail = new MailMessage();
newMail.To="mail Id to whom you want to send password";
newMail.Subject="Subject";
newMail.Body="Password and other content ";
SmtpClient SmtpSender = new SmtpClient();
SmtpSender.Port = 25;
SmtpSender.Host = "mail.YOURDOMAIN.com";
SmtpSender.Send(newMail);

try this
 
Share this answer
 
Comments
Computer Wiz99 15-Nov-13 8:15am    
dbg1912, This is the code I have so far. Is it correct?
[no name] 15-Nov-13 8:18am    
this code is correct only you have to change the value according to your solution..
Computer Wiz99 15-Nov-13 10:29am    
I just uploaded a new code. Check it out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900