Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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
 
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 15-May-12 1:47am
Edited 15-May-12 1:49am
v2
Rate this: bad
good
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:
message.To.Add(dt.Rows[i][1].ToString());
(I'm not sure about index "1", you didn't list column names in your query).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
dineshdena at 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 at 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
0 OriginalGriff 277
1 Maciej Los 230
2 BillWoodruff 205
3 DamithSL 165
4 Sergey Alexandrovich Kryukov 155
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 15 May 2012
Copyright © CodeProject, 1999-2014
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