Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
protected void LinkButton1_Click(object sender, EventArgs e)
    {
        
        Random rnd = new Random();
        int ram = rnd.Next(1111, 9999);

         SqlConnection con = new SqlConnection("Data Source=(local);Integrated Security=true;Initial Catalog=ProPOSDB");

        // Gmail Address from where you send the mail
        var fromAddress = "checkingforasp@gmail.com";
        // any address where the email will be sending
        con.Open();
        SqlDataAdapter ad = new SqlDataAdapter("SELECT UserName,Password FROM UserInfo Where Email ='" + TextBox1.Text + "'", con);

        DataTable dt = new DataTable();
        ad.Fill(dt);
        con.Close();


        var toAddress = dt.Rows[0][0].ToString();
        //Password of your gmail address
        //const string fromPassword = "yahoo123.com";
        // Passing the values and make a email formate to display
        string subject = "Password";
        string body = ram.ToString();

        SmtpClient client = new SmtpClient();
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        System.Net.NetworkCredential nc = new System.Net.NetworkCredential("noman.szic@gmail.com", "pass");
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        client.Credentials = nc;

        // Passing values to smtp object

        client.Send(fromAddress, toAddress, subject, body);



        con.Open();
        SqlDataAdapter ad1 = new SqlDataAdapter("update UserInfo set Password = '" + ram + "' where UserName = '" + TextBox1.Text + "'", con);

        DataTable dt1 = new DataTable();
        ad1.Fill(dt1);
        con.Close();
    }


Facing error
Server Error in '/ForgotPass' Application.
The specified string is not in the form required for an e-mail address.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: The specified string is not in the form required for an e-mail address.

Source Error:
Line 52:         // Passing values to smtp object
Line 53: 
Line 54:         client.Send(fromAddress, toAddress, subject, body);
Line 55: 
Posted
Updated 14-Oct-14 13:45pm
v2
Comments
[no name] 14-Oct-14 16:13pm    
Okay, the error message is pretty clear and tells you exactly what the problem is and where it occurs... So what is the question?
Noman Suleman 15-Oct-14 14:47pm    
The specified string is not in the form required for an e-mail address.
Noman Suleman 15-Oct-14 14:48pm    
The specified string is not in the form required for an e-mail address.
Noman Suleman 15-Oct-14 14:48pm    
my question
how to resolve this
The specified string is not in the form required for an e-mail address.
Sergey Alexandrovich Kryukov 14-Oct-14 16:19pm    
You cannot send "forgot password", because, if a password is forgotten, no one in the world knows it.
And if your passwords are stored and can be retrieve, it means that you are not loyal to your customers. Passwords should not be retrievable.
—SA

1 solution

Sergey is absolutely correct. Let me summarize all comments and put my thoughts.

You should store the encrypted version of password, not the exact password, in your database. When somebody forget password, you will ask for the email id or other details according to your business logic. Then you send one reset link with a temporary password (if you can), by which the user can provide one new password for the account.

Now, you take this new password and store it again in database in encrypted format. Don't store the exact password in plain text format.

Coming to the exception...
Quote:
Exception Details: System.FormatException: The specified string is not in the form required for an e-mail address
Seems like issue is with the toAddress. Debug and see what is the value in the below line.
C#
var toAddress = dt.Rows[0][0].ToString();
 
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