Click here to Skip to main content
12,997,441 members (59,754 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 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

        SqlCommand cmd = new SqlCommand("select * from emailsubscribe", con);
        SqlDataAdapter ada = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        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;
                    SmtpClient client = new SmtpClient();
                    client.Host = "";
                    client.Port = 587;
                    client.EnableSsl = true;
                    NetworkCredential credential = new NetworkCredential("", "password");
                    client.Credentials = credential;
                catch (Exception ex)
                    Label1.Text = ex.Message;
Posted 15-May-12 0:47am
Updated 15-May-12 0:49am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:

(I'm not sure about index "1", you didn't list column names in your query).
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

if you use this code then
the problem is that u r using


but you should use


also you can use

dt.Rows[i][1].ToString() in place of grid view
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170622.1 | Last Updated 15 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100