Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone
I am trying to send bulk of mails but i only receive 500 mails not matter how many mails i send.I am sending mails in loop.Here is a sample.
C#
 foreach (EmailInfo data in emaildata.ToArray())
        {
            
           
            
                SmtpClient smtpClient = new SmtpClient();
                MailMessage mailMsg = new MailMessage();
                /////////other stuff for mail here///////
                smtpClient.Send(mailMsg);
                
                smtpClient.Dispose();

}

I am using .net framework 4.0.
I get this exception after 500 mails are sent

"Service not available, closing transmission channel. The server response was: too many connections"
Please help
Thanks.
Posted
Updated 15-Jul-12 19:39pm
v2
Comments
[no name] 16-Jul-12 10:25am    
It's more likely that your mail server is shutting it down unless you implemented your own mail server.

I'm guessing TCP sockets are lingering ...

Why don't you instantiate one smtpClient outside the loop, and reuse it inside the loop?
 
Share this answer
 
For sending bulk emails, there is a process for queuing and a separate process for sending. This leads to issues with large number of recipients. Suggestion would be to use Windows service or an external service to do it rather than ASP.NET.

Following sample can be of help: Simple Windows Service which sends auto Email alerts[^]


Couple of discussion threads here sums up all:
http://stackoverflow.com/questions/3750485/bulk-mailing-using-smtp-server-in-asp-net[^]
http://stackoverflow.com/questions/1198008/send-mass-emails-with-asp-net[^]
 
Share this answer
 
this code is folly tested and used in live environment ;


MailMessage mailObj = new MailMessage();
         SmtpClient clientObj = new SmtpClient();

C#
foreach (EmailInfo data in emaildata.ToArray())
       {// write some code here

clientObj.Send(
mailObj);


}
i hope this code will solve your problem if not please comments.
 
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