Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to send mail to multiple mailid. the mailid are in database.i tried this by binding the mailid database in gridview.and from gridview in getting the cell value into the textbox.And im using that textbox as my to address of my mail id.in this im getting only the first mailid data in the gridview and the mail send to that mailid its not getting next next mailid data i dnt know why? plz help me

C#
con.Open();
        SqlCommand cmd = new SqlCommand("select * from emailsubscribe", con);
        SqlDataAdapter ada = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        ada.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            for (i = 0; i < dt.Rows.Count; i++)
            {
                TextBox1.Text = GridView1.Rows[0].Cells[1].Text.ToString();
                StringBuilder sb = new StringBuilder();
                MailMessage message = new MailMessage();
                message.From = new MailAddress(TextBox1.Text);
                message.To.Add(new MailAddress(TextBox1.Text));
                message.Subject = "mail subscribe";
                message.Body = "welcome";
                message.IsBodyHtml = true;
                try
                {
                    SmtpClient client = new SmtpClient();
                    client.Host = "smtp.gmail.com";
                    client.Port = 587;
                    client.EnableSsl = true;
                    NetworkCredential credential = new NetworkCredential("id@gmail.com", "password");
                    client.Credentials = credential;
                    client.Send(message);
                }
                catch (Exception ex)
                {
                    Label1.Text = ex.Message;
                }
            }
Posted
Updated 15-May-12 0:49am
v2

You don't need GridView and TextBox to send emails, all you need is in you DataTable dt. Also you have set "From" and "To" to same address. Try with:
C#
message.To.Add(dt.Rows[i][1].ToString());

(I'm not sure about index "1", you didn't list column names in your query).
 
Share this answer
 
if you use this code then
the problem is that u r using

GridView1.Rows[0].Cells[1].Text.ToString();

but you should use

GridView1.Rows[i].Cells[1].Text.ToString();


also you can use

dt.Rows[i][1].ToString() in place of grid view
 
Share this answer
 
Comments
dineshdena 15-May-12 9:40am    
thanku sir its solved but its showing another error //Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index// will u plz help me
Technoses 15-May-12 12:01pm    
if you are using GridView1.Rows[i].Cells[1].Text.ToString();
then first of all you should bind Grid
then use it other wise you should use dt

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