0

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

1
Contributor
1
Reply
4
Views
4 Years
Discussion Span
Last Post by HunainHafeez
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.