Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to send mail to large number fast asp.net....It takes some time for me...My code as follows:
C#
string s14 = "select  email from  CandidateReg1";
         // string s14 = "select top 2 email from  CandidateReg1";
           DataTable d7 = cn.viewdatatable(s14);

           for (int m = 0; m < d7.Rows.Count; m++)
           {
               email = d7.Rows[m]["email"].ToString();
               sendmail();

           }

XML
private void sendmail()
    {
        string s43 = "select top 1 mailid,password from EmailTable order by id desc";
        DataTable d47 = cn.viewdatatable(s43);
        for (int k = 0; k < d47.Rows.Count; k++)
        {

            companymailid = d47.Rows[k]["mailid"].ToString();
            password = d47.Rows[k]["password"].ToString();

        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = ConfigurationManager.AppSettings["SMTPServer"];
        //smtp.Port = 587;
        //smtp.EnableSsl = true;
        //smtp.UseDefaultCredentials = true;
        //if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SMTPCredentialUser"]) && !string.IsNullOrEmpty(ConfigurationManager.AppSettings["SMTPCredentialPass"]))
        //{
        //    smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SMTPCredentialUser"], ConfigurationManager.AppSettings["SMTPCredentialPass"]);
        //}
        smtp.Credentials = new System.Net.NetworkCredential(companymailid, password);

        MailMessage message = new MailMessage();
        // message.From = new MailAddress(companymailid);
        message.From = new MailAddress(companymailid);
        message.Subject = "Job Published";
        message.IsBodyHtml = true;
        StringBuilder body = new StringBuilder();
        body.Append("<table>");

        //body.Append("<tr><td>" + pass + "</td></tr>");

        body.Append("<tr><td>Published Job " + Session["jobtitle"].ToString() + ' ' + "with JobRefId (" + ' ' + Session["jobrefid"].ToString() + ')' + "</td></tr>");

        body.Append("</table>");
        body.Append("<br />");
        body.Append("<br />");
        body.Append("Sincerely,<br />");
        body.Append("<span style=\"font-weight: bold; color: #000099;\">Team - ResumeManager</span>");
        body.Append("<br />");
        body.Append("<br />");

        message.Body = body.ToString();
        message.To.Add(email);

        //message.To.Add("korathualex@gmail.com");
        try
        {
            smtp.Send(message);
            popupConfirm.ShowOnPageLoad = false;
            PopUpSendMailMsg.ShowOnPageLoad = true;
            lblmsg.Visible = false;
            lblmsgsendmail.Visible = true;
            lblmsgsendmail.Text = "Mail Send Successfully";

        }
        catch (Exception ex)
        {
            cn.WriteError(ex.Message);
        }
    }
Posted
Comments
Sandeep Mewara 6-Feb-13 12:21pm    
In what range is your 'large number'?
Sergey Alexandrovich Kryukov 6-Feb-13 12:49pm    
"Large number" is not a valid e-mail address, anyway. :-)
—SA

Rewrite the code added in sendmail function to get the credential, It will save time to get email credential from database everytime.

C#
string s43 = "select top 1 mailid,password from EmailTable order by id desc";
DataTable d47 = cn.viewdatatable(s43);
companymailid = d47.Rows[0]["mailid"].ToString();
password = d47.Rows[0]["password"].ToString();

 for (int m = 0; m < d7.Rows.Count; m++)
           {
               email = d7.Rows[m]["email"].ToString();
               sendmail(companymailid,password,email );

           }
 
Share this answer
 
just remove
C#
string s43 = "select top 1 mailid,password from EmailTable order by id desc";
       DataTable d47 = cn.viewdatatable(s43);
       for (int k = 0; k < d47.Rows.Count; k++)
       {

           companymailid = d47.Rows[k]["mailid"].ToString();
           password = d47.Rows[k]["password"].ToString();

       }


from sendmail and add it before calling sendmail function.
 
Share this answer
 
Comments
_Amy 6-Feb-13 23:50pm    
I don't think it is going to make much difference..
[no name] 7-Feb-13 0:00am    
You can check the solution 3, it is the same what I suggest you.
_Amy 7-Feb-13 1:32am    
Solution 4 is not there. I think it's deleted. :)
[no name] 7-Feb-13 1:34am    
You can check the solution 3, it is the same what I suggest you.
One way to do large number of tasks quickly is by threading.
But you have to be very careful with this technique.

Microsoft has provided its Thread Pool Manager class to ease the things out.

http://msdn.microsoft.com/en-us/library/0ka9477y.aspx[^]
 
Share this answer
 
Comments
Jason Gleim 6-Feb-13 22:11pm    
Yea... especially since this is in asp according to the op. You can quickly make the web server cry if you are spinning a bunch of threads off of a session thread as you'll hold all the server resources until every thread completes and gets released (even using the thread pool). Scalability becomes a problem quickly.

I don't know how much support there is or isn't for parallel tasks in asp.net apps but that might be a better route. The parallel task libraries know how to break up the task according to the server architecture optimizing for number of cores and such. Much smarter than the thread pool and, IMHO, easier to write against.
Basically, it depends on your Server Capability and Network Speed. In this situation I can see, you are facing the problem just because the number of rows is more in datatable. You should go for threads or some asynchronous call. Use some processing object to show user(Sending Mail is on progress).


--Amit
 
Share this answer
 

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