Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
I want to send mail to large number fast asp.net....It takes some time for me...My code as follows:
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();
 
           }
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 6-Feb-13 6:01am
Comments
Sandeep Mewara at 6-Feb-13 12:21pm
   
In what range is your 'large number'?
Sergey Alexandrovich Kryukov at 6-Feb-13 12:49pm
   
"Large number" is not a valid e-mail address, anyway. :-)
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Rewrite the code added in sendmail function to get the credential, It will save time to get email credential from database everytime.
 
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 );
 
           }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

just remove
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.
  Permalink  
Comments
_Amy at 6-Feb-13 23:50pm
   
I don't think it is going to make much difference..
Mike Thom at 7-Feb-13 0:00am
   
You can check the solution 3, it is the same what I suggest you.
_Amy at 7-Feb-13 1:32am
   
Solution 4 is not there. I think it's deleted. :)
Mike Thom at 7-Feb-13 1:34am
   
You can check the solution 3, it is the same what I suggest you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
Jason Gleim at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 205
1 Sergey Alexandrovich Kryukov 160
2 jlopez788 109
3 Rob Philpott 100
4 Prakriti Goyal 97
0 OriginalGriff 6,837
1 Sergey Alexandrovich Kryukov 5,589
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 6 Feb 2013
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