I have a scenario, An MSMQ queuing system...Records are queued on a timely basis...A WCF listener that listens to the queue, starts processing the records as soon as the records are queued up...does some process and sends email after that(There are 10 queues and 10 listeners out of which 3 listeners are responsible for email sending). The problem I am facing is the email sending part, where larger data is queued up then for some records I get the following error
Service not available, closing transmission channel. The server response was: 4.3.2 The maximum number of concurrent connections has exceeded a limit, closing transmission channel
The class that sends email is
public class A
{
//Method is static as it is a common method used by other processes running in parallel
public static void SendMail()
{
MailMessage mail = new MailMessage();
SmtpClient client = new SmtpClient();
///Email information goes here
client.Send(mail);
}
}
I guess even my method is static the smtp object is instantiated each time which causes the problem. Even if I increase the concurrent connections it is not solving the problem. I have a couple of workaround but required some more light on this.
- Limit the no. of concurrent connections to let say 100. So even I have 1000 records queued up and the listeners starts processing them in parallel the smtp process will not use more than 100 connections at a time, wait for it to complete and then take up the next 100 and so on. But I am not sure how to do this.
- Use parallel foreach loop or SmtpClient.SendAsync method, but here also my proficiency level is not much regarding these methods, so I am a little bit afraid(I need to make sure there is no major performance hit). So just needed a stable and better approach to solve this.