i am getting error

ERROR: The specified string is not in the form required for an e-mail address.

but when i pass static email address , it works , why ?

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(connectionString);
        String command = "select visitorEmailID from tblvisitorSubscriptions";
        SqlCommand sqlCom = new SqlCommand(command, con);
        SqlDataAdapter sqlDA = new SqlDataAdapter(command, con);
        DataTable DT = new DataTable();
        try
        {
            con.Open();
            sqlCom.ExecuteReader();
            sqlDA.Fill(DT);

            foreach (DataRow dr in DT.Rows)
            {
                String Dr = Convert.ToString(dr["visitorEmailID"]);
                Response.Write("Email:" + "\"" + Dr + "\"");

                //Email sending part
                MailDefinition mailDefinition = new MailDefinition();
                mailDefinition.From = "hunain.hafeez@gmail.com";

                MailMessage message = new MailMessage();
                message.From = new MailAddress("hunain.hafeez@gmail.com");
               message.To.Add(new MailAddress( Dr ));
                message.Subject = "Subscription";
                message.IsBodyHtml = true;

                string strBody = "<div style=\"background-color:skyblue\"></div><b>Subscribed</b>" +
                                 " <font color=\"red\">ASP.NET</font>";
                message.Body = strBody;

                SmtpClient objClient = new SmtpClient("smtp.gmail.com", 587);
                objClient.EnableSsl = true;


                objClient.Credentials = new System.Net.NetworkCredential("hunain.hafeez", "********");

                objClient.Send(message);
                //Response.Write("<br/>" + "Mail Sent to your email id successfully..!!");
            }
        }
        catch (Exception exc)
        {
            Response.Write("ERROR:" + " " + exc.Message);
        }
        finally
        {
            con.Close();
        }
    }

check the underlined part, it is causing error

solved

visitorEmailID had a value that wan't in email address format.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.