Click here to Skip to main content
15,890,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting error while sending email "The specified string is not in the form required for an e-mail address." plz help me

Below is the code

C#
string strConnection= System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

       // string strSelect = "SELECT Username,password FROM registration WHERE emailid = @emailid";
        string strSelect = "SELECT name FROM registration WHERE email_id = @email_id";
 
         SqlConnection connection = new SqlConnection(strConnection);
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.Text;
        command.CommandText = strSelect;

        //SqlParameter email = new SqlParameter("@emailid", SqlDbType.NVarChar,100);
        SqlParameter email = new SqlParameter("@email_id", SqlDbType.VarChar, 100);
        email.Value = txtEmail.Text.Trim().ToString();
        command.Parameters.Add(email);
      
        DataSet dsPwd = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter(command);
        connection.Open();
        dAdapter.Fill(dsPwd);
        connection.Close();
        if(dsPwd.Tables[0].Rows.Count > 0 )
        {
            MailMessage loginInfo = new MailMessage();
            //loginInfo.To.Add(txtEmail.Text.ToString());
            loginInfo.To.Add(new MailAddress(txtEmail.Text));
            loginInfo.From = new MailAddress("mail@gmail.com");
            loginInfo.Subject = "Forgot Password Information";
 
            loginInfo.Body = "Username: " + dsPwd.Tables[0].Rows[0]["Username"] + "<br><br>Password: " + dsPwd.Tables[0].Rows[0]["password"] + "<br><br>";
            loginInfo.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("mymail@gmail.com", "password");
            smtp.Send(loginInfo);
            lblmsg.Text ="Password is sent to you email id,you can now Login";
        }
        else
        {
            lblmsg.Text = "Email Address Not Registered";
        }
    }
Posted
Updated 25-Oct-13 21:07pm
v2
Comments
[no name] 26-Oct-13 3:01am    
Post the piece of code else it's difficult to identify the error...
sanjaysgh 26-Oct-13 3:05am    
string strConnection= System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

// string strSelect = "SELECT Username,password FROM registration WHERE emailid = @emailid";
string strSelect = "SELECT name FROM registration WHERE email_id = @email_id";

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

//SqlParameter email = new SqlParameter("@emailid", SqlDbType.NVarChar,100);
SqlParameter email = new SqlParameter("@email_id", SqlDbType.VarChar, 100);
email.Value = txtEmail.Text.Trim().ToString();
command.Parameters.Add(email);

DataSet dsPwd = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter(command);
connection.Open();
dAdapter.Fill(dsPwd);
connection.Close();
if(dsPwd.Tables[0].Rows.Count > 0 )
{
MailMessage loginInfo = new MailMessage();
//loginInfo.To.Add(txtEmail.Text.ToString());
loginInfo.To.Add(new MailAddress(txtEmail.Text));
loginInfo.From = new MailAddress("mail@gmail.com");
loginInfo.Subject = "Forgot Password Information";

loginInfo.Body = "Username: " + dsPwd.Tables[0].Rows[0]["Username"] + "<br><br>Password: " + dsPwd.Tables[0].Rows[0]["password"] + "<br><br>";
loginInfo.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("mymail@gmail.com", "password");
smtp.Send(loginInfo);
lblmsg.Text ="Password is sent to you email id,you can now Login";
}
else
{
lblmsg.Text = "Email Address Not Registered";
}
}
sanjaysgh 26-Oct-13 3:07am    
plz help me
[no name] 26-Oct-13 3:08am    
Which line you are getting the error..?
sanjaysgh 26-Oct-13 3:13am    
here i am getting error :
loginInfo.To.Add(new MailAddress(txtEmail.Text));

1 solution

We can't help you make your users type correctly!
If what they type is not a valid email address[^] then it will be rejected.

Things to try:
1) Trim it - you do that when you save the data into your DB, so why not use the trimmed version as the actual address?
2) Validate it. Look at the link above and check the data. If it is invalid, then don't save it in your DB, just report the problem to the user!
 
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