Click here to Skip to main content
15,900,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that I have a textbox for the user to enter their email address so that they can get their password, if they forget it, sent to the user email. I have the code but it doesn't seem to work. I have a mail server for the emails. Mail server is mail.fastfix.com. What is wrong in my code the reason why the email won't send?

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["HotConnectionString"].ConnectionString;
        string strSelect = "SELECT EmailAddress, Password FROM Tablepass WHERE EmailAddress ='" + TextBoxEA.Text + "'";

        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@fastfix.com");
            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 = "smtp.mail.fastfix.com";
            smtp.Port = 25;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("YourEmailID@fastfix.com", "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
Comments
joshrduncan2012 19-Nov-13 9:10am    
I suggest wrapping the entire code block in a try/catch block and going under the debugger to see where the code fails.
Computer Wiz99 19-Nov-13 9:32am    
Ok, Testing now.
Computer Wiz99 19-Nov-13 9:36am    
It fails at this line: loginInfo.From = new MailAddress("YourID@fastfix.com");. The error says: The specified string is not in the form required for an e-mail address. What does this mean? All of the users do not have the same email address ending. I just want to use my mail server to send an email to the user with their password in it.
joshrduncan2012 19-Nov-13 9:52am    
A couple of things I would try:

1) This line:
string strSelect = "SELECT EmailAddress, Password FROM Tablepass WHERE EmailAddress ='" + TextBoxEA.Text + "'";
(I would add .Trim() to the end of TextBoxEA.Text to cut down on whitespace leading and trailing when an email address is entered in).

2) This segment:
SqlParameter email = new SqlParameter("@EmailAddress", SqlDbType.VarChar, 50);
email.Value = TextBoxEA.Text.Trim().ToString();
command.Parameters.Add(email);
(This segment looks suspicious. You may want to change "command.Parameters.Add(email)" to "command.Parameters.AddWithValue(email)".)
Computer Wiz99 19-Nov-13 10:16am    
Ok. I have made the changes from what you said but the command.Parameters.AddWithValue(email) is a No overload for the method "AddWithValue" takes 1 arduments. In my database all of the users have different emails. Example: hotmail.com, gmail.com, yahoo.com and others. Even school email address. I there a way to use these emails to send the password to the user through the mail server?

1 solution

Check the TO field and make sure the email address is in the correct format and the field is not empty.
 
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