Limit outbound SMTP connections

Posted on 2008-11-11
Medium Priority
Last Modified: 2012-05-05

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.

Question by:Danik_S
  • 2
  • 2
LVL 10

Expert Comment

ID: 22955820
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.

Author Comment

ID: 23436038
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.
LVL 10

Accepted Solution

slamhound earned 1000 total points
ID: 23436560
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.

Author Closing Comment

ID: 31515724
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.

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction ( All good things must come to an end ) The original MySQL API has gone away.  It was deprecated (years ago) by PHP in Version 5.5, and removed from PHP in all current releases .  As a result, the issue is upon us today.  It's time to …
Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question