What is the best way to sending mail from gridview check box checked row

Gani_tpt 2,046 Reputation points
2024-06-18T08:37:52.73+00:00

I am using gridview and it contains checkboxes.

when i click submit button, i am sending mail to the customer for every checking record.

if i save record without mail, data will be saving more accurate and fast.

But, when i send mail for every records from the gridview, it's taking long time to process it and done.

I know the reason, it will hit smtp server every records and return back is taking time in the server.

What is the alternative way to sending mail while submit the records...

below is my complete code for reference.

 protected void Submit_Click(object sender, EventArgs e)
        {
            try
            { 		
		System.Threading.Thread.Sleep(3000);
                    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow gvrow in gbCustomer.Rows)
                    {
                        if (gvrow.RowType == DataControlRowType.DataRow)
                        {
                            var checkbox = gvrow.FindControl("CheckBox1") as CheckBox;
                            if (checkbox.Checked)
                            {
				var Id = (gvrow.FindControl("hdnctrlId") as HiddenField).Value;
                                var CustNo = (gvrow.FindControl("hdnCustNo") as HiddenField).Value;
                                var CustName = (gvrow.FindControl("hdnCustName") as HiddenField).Value;
                                var DOJ = (gvrow.FindControl("hdnDOJ") as HiddenField).Value;
                                //********* Sending Mail
                                SendMail(Id, CustNo, CustName, DOJ);
                                //********* Sending Mail
                             
                                CheckBox chk = (CheckBox)gvrow.FindControl("CheckBox1");
                                chk.Checked = false;
                                CheckBox chkAll = (CheckBox)gbCustomer.HeaderRow.FindControl("chkAll");
                                chkAll.Checked = false;
                            }
                        }
                    }
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Mail Sent successfully.!');window.location.href='NewCustomer.aspx';", true);
	     }
            catch (Exception ex)
            {
                ErrorMessage.Text = ex.Message;
                ErrorMessage.Visible = true;
            }
            finally
            {
                
            }
        }
  private void SendMail(string Id, string CustNo, string CustName, string DOJ)
        {
            try
            {
                FromEmail = "axl1987@biech.com";
                ToEmail = "bjm21765@biech.com";
                if (!string.IsNullOrEmpty(ToEmail))
                {
                    MailMessage mm = new MailMessage(FromEmail, ToEmail);
                    mm.Subject = "Reg : Customer Request";
                    string strhtml = "<HTML>";
                    strhtml += "<body>";
                    strhtml += "<br>";
                    strhtml += "<table>";
                    strhtml += "<tr>";
                    strhtml += "<td>";
                    strhtml += "Cust. Name ";
                    strhtml += "</td>";
                    strhtml += "<td>";
                    strhtml += "DOJ. ";
                    strhtml += "</td>";
                    strhtml += "<tr><td>";
                    strhtml += "&nbsp;" + CustName + "  </td>";
                    strhtml += "<td>";
                    strhtml += "&nbsp;" + DOJ + "  </td>";
                    strhtml += "<br>";
                    strhtml += "<div align='left'>Regards,</div>";
                    strhtml += "<div align='left'>Alex Paul</div>";
                    strhtml += "</body>";
                    strhtml += "</html>";
                    mm.Body = strhtml;
                    mm.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Send(mm);
                }
            }
            catch (Exception ex)
            {
                Response.Write("Error occurred: " + ex.Message);
            }
        }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,374 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,501 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 27,956 Reputation points Microsoft Vendor
    2024-06-19T02:43:50.62+00:00

    Hi @Gani_tpt,

    It depends on network speed + sending server load + receiving server load.

    You could try using async.Or you could consider popping up a notification stating that the email has been sent to the user later, rather than forcing them to wait.

    The following code example demonstrates sending an email message asynchronously.

    https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=net-8.0&redirectedfrom=MSDN#examples

    protected void SendAsyncEmail(object sender, EventArgs e)
    {
        string to = txtTo.Text;
        string from = txtEmail.Text;
        string password = txtPassword.Text;
        string subject = txtSubject.Text;
        string body = txtBody.Text; 
        Thread email = new Thread(delegate()
        {
            SendEmail(to, from, password, subject, body);
        });
     
        email.IsBackground = true;
        email.Start();
        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
    }
     
    private void SendEmail(string to, string from, string password, string subject, string body)
    {
        using (MailMessage mm = new MailMessage(from, to))
        {
            mm.Subject = subject;
            mm.Body = body;
            mm.IsBodyHtml = false;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential(from, password);
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments