SMTPSecureSocket.SendMail gives 503 error "Send Hello First"

I’m using a SMTPSecureSocket in a standalone Web app. It works fine when sending a single message, but when multiple users are using the site, at some point I start getting SMTP errors: “503 Send Hello First”.

I believe that what’s happening is I’m calling SMTPSecureSocket.SendMail() while sending is already in progress and this confuses things.

I’m not sure how to prevent this:

  • do I check SMTPSecureSocket.messages.ubound() to see if a message is in the queue?
  • do I use the .MailSent() event to set a boolean and only call .SendMail() again if true?
  • do I check RemoteAddress or some other property?
  • Use a separate socket per session.
  • use a helper app

Interesting. I specifically made it a singleton (as a property of the WebApp class) because I assumed that it would be better/safer to have one SMTPSecureSocket in charge of sending emails, rather than potentially multiple of them running per-session.

I would worry that having multiple sockets hitting the same SMTP server at once from the same IP address might be something to trigger anti-spam countermeasures, though that’s just a guess.

Build a queue and send emails so that only one socket is working at a given time. I do this but with HTTPSockets for a different task, but the purpose is still to respect the server and not be spammy with requests.

I ended up staying with my singleton design, with a single socket as a property of the WebApp class. But I made it a subclass and added some wrapper functions.

Basically it sets a “busy” flag when it starts sending messages, and clears the flag when messages are all sent (or an error occurs).

If you try to add a new message to the queue while busy, it just sets a 1 second timer and tries again later. In simple testing it seems to work, but of course many things work well with simple testing…