Limit outbound SMTP connections


I have inherited a  classic ASP web application that sends out email through a remote mail server.  The remote mail server allows a maximum of 5 connections at any one time.  Messages exceeding that limit are returned to the web server with the "... too many connections" failure notice.  The website is hosted by a hosting vendor in a shared environment.

The website sends mail using a variety of components.  Mostly Persits' ASPEMail and CDOSYS, from memory there is some old CDONTS code in there too.

How do I limit the amount of outbound SMTP connections to a maximum of 5 at any one time?  Obviously if the limit is reached I want messages to wait and then go out as soon as possible.

Many thanks in advance.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

One idea is to simply keep a track of what connections your code is opening up. If it's multiple sections of code sending the email you could get each bit of code to read/write to a single text file. If the text file has the number 5 in it, keep reading it till it drops to 4 or less. If it's 4 or less, increment it by one, send the message, clean up and then decrement it by one.

But for each sending method, you might create the connection and check for an error code. If you get the error code, get it to wait.
Danik_SAuthor Commented:
Sorry for being out of touch.

Thanks for your response slamhound.  I don't think it would work though because in the case of ASPEmail the messages are passed to a queue which is managed by the web server, not the web application.  So my application wouldn't know how many connections the queue has open to my smtp server.
What I've suggested is sort of a manual way of counting the number of connections. Every time your code sends an email you must assume that it counts as one connection. So you would add one to a system global variable (like a central text file or database field). Your code checks this variable and if it's not 5, increments it and sends the email. If it does equal 5, you give the wait message and try again. You can then use your code to slow down/stop sending emails during peak periods ie. when the number of connection counts reaches 5.

You can then decrement this variable once the message has been sent or on a timer. eg. 1 minute after the email was sent.

It's a bit messy as it assumes that no other code is sending to that queue and that your code is closing all of the connections appropriatly.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Danik_SAuthor Commented:
Thanks Slamhound.  As you say it's not appropriate for apps where other code is sending to the same queue.  So it's not for me but could easily work for someone else.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.