Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form the a user that has forgotten their password can enter in their email address and have it sent to their email. It works but the user gets the email twice. Why is this happening? Is it something in my code?

C#
protected void btnPass_Click(object sender, EventArgs e)
    {
        //Create Connection String And SQL Statement
        string strConnection = ConfigurationManager.ConnectionStrings["HOTConnectionString"].ConnectionString;
        string strSelect = "select INST_ID, EmailAddress, Password from Tablepass where EmailAddress ='" + TextBoxEA.Text.Trim() + "'";

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

        SqlParameter email = new SqlParameter("@EmailAddress", TextBoxEA.Text.Trim());
        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("Administrator@sacscoc.org", "Southern Association of Colleges and Schools Commission on Colleges");
            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("mail.fastfix.com");
            smtp.Host = "mail.fastfix.com";
            smtp.Port = 25;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("@EamilAddress", "YourEmailPassword");
            smtp.Send(loginInfo);
            lblMessage.Text = "Password is sent to your email";

            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
Comments
joshrduncan2012 20-Nov-13 9:18am    
You have 2 instances of this line:

smtp.Send(loginInfo);
Computer Wiz99 20-Nov-13 9:21am    
Thanks!! Which one do you think is safe to remove?
joshrduncan2012 20-Nov-13 9:23am    
That's totally up to you, but I'd remove the one above the try/catch block and move this statement: lblMessage.Text = "Password is sent to your email"; to be after the instance within the try/catch block.
Computer Wiz99 20-Nov-13 9:25am    
Ok. I see what you are saying. I will put an update up in a few.

You have 2 instances of this line: smtp.Send(loginInfo);
 
Share this answer
 
Comments
RaisKazi 20-Nov-13 12:20pm    
My 5!
joshrduncan2012 20-Nov-13 12:21pm    
Thanks!
The Updated code:


C#
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("mail.fastfix.com");
            smtp.Host = "mail.fastfix.com";
            smtp.Port = 25;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("@EamilAddress", "YourEmailPassword");
            
            try
            {
                smtp.Send(loginInfo);
                lblMessage.Text = "Password is sent to your email";
            }
            catch (Exception ex)
            {

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

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

    }

    
}
 
Share this answer
 
joshrduncan2012 spotted your code issue very well. But your code still has a potential Web Secutity issue. You are concatenating user input to your inline SQL query. This is an open invitation for SQL Injection.

Have a look at below links for more information on SQL Injection.

SQL Injection and Cross-Site Scripting

http://en.wikipedia.org/wiki/SQL_injection
 
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