Limit outbound SMTP connections

Posted on 2008-11-11
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
    LVL 10

    Expert Comment

    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

    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

    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

    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

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    A Change in PHP Behavior with Session Write Short Circuit ( (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now