Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Configuration;

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

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try {
            DataSet ds = new DataSet();
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT UserName, Password FROM Users Where Email='" + txtEmail.Text.Trim() + "'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }
            if (ds.Tables[0].Rows.Count>0)
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(txtEmail.Text);
                msg.To.Add(txtEmail.Text);
                msg.Subject="Share It: Password Recovery";
                msg.Body="Hi, " + ds.Tables[0].Rows[0]["FirstName"] +" " + ds.Tables[0].Rows[0]["LastName"] +"<br />As requested, here is your login details<br /><br />Your Username: " + ds.Tables[0].Rows[0]["UserName"] + "<br /><br />Your Password: " + ds.Tables[0].Rows[0]["Password"] + "<br /><br />Thank you for trusting us! Hope to hear from you soon!<br /><br />";
                msg.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient("smtp.gamil.com", 587);
                smtp.UseDefaultCredentials = false;
                //smtp.Host = "smtp.gmail.com";
                //smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("theresakang93@gmail.com", "passwordremoved");
                smtp.EnableSsl = true;
                smtp.Send(msg);
                lbltxt.Text = "Your Password Details Sent to your mail.";
            }
        
            else
            
                lbltxt.Text = "The Email you entered does not exists.";
        }    
        catch (Exception ex) {
        
                Console.WriteLine("{0} Exception caught.", ex);
                lbltxt.Text = "Unable to send";
            }
Posted
Updated 8-Jan-20 22:05pm
v3
Comments
Ron Beyer 9-Jan-14 23:36pm    
"Thank you for trusting us", I like that, even though you store passwords in clear text in your database, are susceptible to SQL injection attacks making it easy to steal all your users passwords and emails, and posted the password to your email account...
TheresaKang93 9-Jan-14 23:45pm    
I understand. But I am just a student trying to do a sample coding for my homework.
Ron Beyer 9-Jan-14 23:47pm    
I'm pretty sure your "from" address has to be the same as your account username for gmail, otherwise its called relaying and I'm pretty sure GMail doesn't allow it.
Sergey Alexandrovich Kryukov 9-Jan-14 23:53pm    
Well, it only makes it even more important to avoid doing bad things when you are just learning. I'll post some links to help you...
—SA
Sergey Alexandrovich Kryukov 9-Jan-14 23:57pm    
But then it's only more important to avoid doing bad things when you are just learning. I added an answer with some references to help you. Please feel free to comment on it and ask some follow-up questions if you have any.
—SA

Having seen this code, I would never use your website, due to all the security holes.

But, if the email is not sent, then your mail server is not set up correctly, not a big leap from how bad this code is.

Nowadays, passwords get stored encrypted, and it is impossible to send someone their password, all you can do, generally, is send a reset link that uses a GUID to associate with a user, or a temp password they need to change the next time they log in.
 
Share this answer
 
Refer my answer - sending email to gmail from asp.net[^].
This works pretty good. Tested.

If you are getting any Exception, please reply me with that. I will, help.
 
Share this answer
 
Comments
Ron Beyer 9-Jan-14 23:56pm    
Got my 5, pretty sure the problem here is the "from" address.
Thanks a lot Ron Beyer... :)
TheresaKang93 10-Jan-14 0:24am    
Thank you so much! :)
Most welcome buddy. :)
Please see the comments to the questions, Ron's and mine.

Please see my past answers for some further detail:
i already encrypt my password but when i log in it gives me an error. how can decrypte it[^],
Decryption of Encrypted Password[^],
storing password value int sql server with secure way[^].

—SA
 
Share this answer
 
Comments
Ron Beyer 9-Jan-14 23:57pm    
+5!
Sergey Alexandrovich Kryukov 10-Jan-14 1:48am    
Thank you, Ron. I hope you saw another OP's response, same thing again, "but im still a beginner, and my teacher asked us to..." This is so pathological...
—SA
Ron Beyer 10-Jan-14 8:56am    
Some people aim to pass, others aim to excel.
Sergey Alexandrovich Kryukov 10-Jan-14 10:26am    
...and some aim to fail :-)
—SA

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