Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an imported excelsheet in GridView. Now i've to send an email to all records but in a way like email is to be sent after selecting top 10 records from Grid View and when the process is complete an email is send again to the next 10 records and so on.

Following is my code:

C#
void Send_Mail()
    {
        try
        {
            string Pass, FromEmailid, HostAdd;

            foreach (GridViewRow gr in DataGridView.Rows)
            {
                HostAdd = ConfigurationManager.AppSettings["Host"].ToString();
                FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();
                Pass = ConfigurationManager.AppSettings["Password"].ToString();

                Label LblName = gr.FindControl("LblName") as Label;
                Label LblMail = gr.FindControl("LblEmail") as Label;

                string Name = LblName.Text;
                string Mail = LblMail.Text;
                string subject = TxtSubject.Text;

                SmtpClient client = new SmtpClient();
                MailMessage msg = new MailMessage();

                NetworkCredential credentials = new NetworkCredential(FromEmailid, Pass);

                client.Host = HostAdd;
                client.Port = 25;
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;
                client.EnableSsl = true;
                MailAddress from = new MailAddress(FromEmailid);
                msg.IsBodyHtml = true;
                msg.Subject = subject;
                msg.Body = ReadTemplate(Name);
                msg.To.Add(Mail);
                msg.From = from;

                client.Send(msg);
                LblMessage.Text = "Email Send Successfully";
                LblMessage.Visible = true;
            }
        }
    }
Posted
Comments
CodingLover 11-Mar-14 2:38am    
That means do you want to send the email only to the selected users?
Nimit Joshi 11-Mar-14 3:15am    
No No not to the selected users. I want to send mail to all records. But when i' ll send the mail to 3000 records(which i've) then the process is too slow. So that i want to send the mail 100 by 100
nandakishoreroyal 11-Mar-14 2:38am    
What is the using of doing that. You are sending email to all the rows right in anyway.

then why need to sort top 10 like that..
Nimit Joshi 11-Mar-14 3:16am    
Actually the records no are 3000. So it is too hectic to send the mail to all records that's why i want to send the mail 100 by 100
Aravindba 11-Mar-14 2:56am    
u need to send top 10 records form gridview row selected,then again send next top 10 records and so on...

OR

u have a email address in a gridview,and if select rows form gridview and get email address form selected row and send mail

1 solution

Hi Try this

In below gridview u need to add checkbox,then only u use gridview.selecteitems
C#
foreach (GridDataItem gdi in usersGridView.SelectedItems) {


  HostAdd = ConfigurationManager.AppSettings["Host"].ToString();
                FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();
                Pass = ConfigurationManager.AppSettings["Password"].ToString();
 
                Label LblName = gr.FindControl("LblName") as Label;
                Label LblMail = gr.FindControl("LblEmail") as Label;
 
                string Name = LblName.Text;
                string Mail = LblMail.Text;
                string subject = TxtSubject.Text;
 
                SmtpClient client = new SmtpClient();
                MailMessage msg = new MailMessage();
 
                NetworkCredential credentials = new NetworkCredential(FromEmailid, Pass);
 
                client.Host = HostAdd;
                client.Port = 25;
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;
                client.EnableSsl = true;
                MailAddress from = new MailAddress(FromEmailid);
                msg.IsBodyHtml = true;
                msg.Subject = subject;
                msg.Body = ReadTemplate(Name);
                msg.To.Add(Mail);
                msg.From = from;
 
                client.Send(msg);
                LblMessage.Text = "Email Send Successfully";
                LblMessage.Visible = true;

//Here delete the data form gridview ,use where condition as LblMail,so it remove row form gridview which are send mail already 
  
}
 
Share this answer
 
v2
Comments
Nimit Joshi 11-Mar-14 3:36am    
Thanks for the answer, but sir it is not allowed to add a checkbox in the GridView.
Aravindba 11-Mar-14 4:12am    
then how u select rows form gridview ?
Nimit Joshi 11-Mar-14 4:50am    
I do want to select the rows. I just want to send them mail 100 by 100
Aravindba 11-Mar-14 5:10am    
ur question is not clear,pls update ur question and description....,if u checkboxes in gridvew then only u can send selected user ,other wise all rows are mail send ,u cant restrict 10 user and then 10 users,other u export excel to database and add one more field in table as status then retrieve 10 user which status as True send mail and update status False, and select other 10 users which status as True and so on
Nimit Joshi 11-Mar-14 6:47am    
got it. thanks

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