Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i am trying to send password of the user accounts using gmail as smtp server..

the problem is that when i sent email to the email address which is not in my database.

it will prompt that email does not exit in the database (see code). but at the same time.. it also sent the email to the email adress with the last recovered password present in datareader to email adrees i have writen in the textbox(i.e which is not in databse) .. which is not in database.

i want a check so no email can sent to the email adresse not in my database

i declared this function in class name my class
C#
<pre>public void getUserPassword(string Email)
    {

        SqlConnection con = new SqlConnection();    //Create an SqlConnection object.

        //Pass the connection string to the SqlConnection object 

        con.ConnectionString = "Data Source=blaPC\\SQLEXPRESS; Initial Catalog=forgetpassword; Integrated Security=True";

        con.Open();  //Open the connection 

        string procedureText = "retriveUserPassword";

        SqlCommand cmd = new SqlCommand(procedureText, con); //Create an SqlCommand object.

        cmd.CommandType = CommandType.StoredProcedure; //Change the behaviour of command text to stored procedure.

        cmd.Parameters.Add(new SqlParameter("@Email", Email));  //Pass value to the parameter that you have created in procedure.

        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {

            
            body = "Your Password is " + dr[0].ToString();
            
        }

        else
        {

            HttpContext.Current.Response.Write("<script>alert('Email does not exist')</script>");
            
            

        }


and here is the send email buttion. i think the problem is in that

protected void Button1_Click(object sender, EventArgs e)
    {
        myclass obj = new myclass();
        //obj.getUserPassword(TextBox1.Text);
        //Label1.Text = myclass.body;

        try
        {
            
            MailMessage mail = new MailMessage();
            obj.getUserPassword(TextBox1.Text);

            mail.To.Add(TextBox1.Text);
            
            mail.From = new MailAddress("farooqspecials@gmail.com");
            mail.Subject = "My Project Password Recovery System";

            string Body = myclass.body;
                          //"using Gmail in ASP.NET";
            mail.Body = Body;

            mail.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential
            ("farooqspecials@gmail.com", "blablabla");
            smtp.EnableSsl = true;
            smtp.Send(mail);
            Label1.Text = "Dear User Your Password is Sent to your Email Address";
        }
        catch (Exception ex)
        {
            Label1.Text = "some Problem" + ex;
        }




store procedure is


C#
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[retriveUserPassword] @Email varchar(50) 

as 

begin 

      select Password from login where Email=@Email 

end
Posted
Updated 5-Oct-11 2:08am
v3
Comments
Tejas Vaishnav 5-Oct-11 7:59am    
What is your procedure..?
codegeekalpha 5-Oct-11 8:07am    
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[retriveUserPassword] @Email varchar(50)

as

begin

select Password from login where Email=@Email

end

change the
C#
public bool getUserPassword(string Email)
{
   bool validEmail = false;
   //do db stuff
        if (dr.Read())
       {
 
            
            body = "Your Password is " + dr[0].ToString();
            validEmail = true;
        }
 
        else
        {
 
            HttpContext.Current.Response.Write("<script>alert('Email does not exist')</script>");
            
            
 
        }

   return validEmail
}

and then in Button click event check if the function returns a validEmail...

protected void Button1_Click(object sender, EventArgs e)
    {
        myclass obj = new myclass();
        //obj.getUserPassword(TextBox1.Text);
        //Label1.Text = myclass.body;

        try
        {
            
            MailMessage mail = new MailMessage();
            if(obj.getUserPassword(TextBox1.Text))
            {
 
            mail.To.Add(TextBox1.Text);
            
            mail.From = new MailAddress("farooqspecials@gmail.com");
            mail.Subject = "My Project Password Recovery System";
 
            string Body = myclass.body;
                          //"using Gmail in ASP.NET";
            mail.Body = Body;
 
            mail.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential
            ("farooqspecials@gmail.com", "blablabla");
            smtp.EnableSsl = true;
            smtp.Send(mail);
            Label1.Text = "Dear User Your Password is Sent to your Email Address";
}
        }
        catch (Exception ex)
        {
            Label1.Text = "some Problem" + ex;
        }.
 
Share this answer
 
v2
Try this one.

Put this code after else part of dr.read()
C#
dr.close();
dr.dispose();
 
Share this answer
 
Here is the another approach which will also work:
public string getUserPassword(string Email)
        {
            string body = "";
            SqlConnection con = new SqlConnection();    //Create an SqlConnection object.
            //Pass the connection string to the SqlConnection object 
            con.ConnectionString = "Data Source=blaPC\\SQLEXPRESS; Initial Catalog=forgetpassword; Integrated Security=True";
            con.Open();  //Open the connection 
            string procedureText = "retriveUserPassword";
            SqlCommand cmd = new SqlCommand(procedureText, con); //Create an SqlCommand object.
            cmd.CommandType = System.Data.CommandType.StoredProcedure; //Change the behaviour of command text to stored procedure.
            cmd.Parameters.Add(new SqlParameter("@Email", Email));  //Pass value to the parameter that you have created in procedure.
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {

                body = "Your Password is " + dr[0].ToString();
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert('Email does not exist')</script>");
            }
            dr.Dispose();
            dr = null;
            return body;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            myclass obj = new myclass();
            try
            {
                MailMessage mail = new MailMessage();
                string body = obj.getUserPassword(TextBox1.Text);
                if (body.Length > 0)
                {
                    mail.To.Add(TextBox1.Text);
                    mail.From = new MailAddress("farooqspecials@gmail.com");
                    mail.Subject = "My Project Password Recovery System";
                    //"using Gmail in ASP.NET";
                    mail.Body = body;
                    mail.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential
                    ("farooqspecials@gmail.com", "blablabla");
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                    Label1.Text = "Dear User Your Password is Sent to your Email Address";
                }
            }
            catch (Exception ex)
            {
                Label1.Text = "some Problem" + ex;
            }
            finally
            {
                myclass = null;
            }
        }
 
Share this answer
 

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