SMTPSecureSocket in PreemptiveThread

Im trying to use SMTPSecureSocket in a PreemptiveThread to send emails from the thread, but I keep getting all kinds of strange Error messages from the SMTPSecureSocket, and occasionally bad crashes of the whole app as well.

I have subclassed the SMTPSecureSocket and have it instantiated within the thread, it is not used anywhere else in the app. Im listening for all the SMTPSecureSocket events to monitor the activities and keeping it alive until it either errors out or sends the email.

It’s not going well at all, and Im starting to think there is something generally wrong with my approach.

Maybe there is some underlying shared aspects that prevents SMTPSecureSockets from running smoothly in PreemptiveThreads, I don’t know? If so, can it be prevented by using CriticalSection or something?

Any thoughts or advice are welcome.

The socket’s events execute on the main thread. All Xojo framework events execute on the main thread, unless otherwise noted.

You can poll the socket in a loop in the thread, which will cause the events to fire inside the thread. However, you don’t have a way to prevent them from firing inside the main thread if the built-in poller happens to notice the change before your thread does. I don’t think it’ll work the way you need.

If you must have SMTP, I think the CURL classes from the MBS plugin can do it. But honestly, I’d avoid SMTP entirely and use a mail delivery service like Postmark, MailGun, or SendGrid. The concern would be securing your API credentials, but you have that same problem with SMTP too. But I don’t know your use-case, so for all I know the user enters their own SMTP credentials.

3 Likes

Fully agree. If the users do not enter the SMTP credentials then Postmark, Mailgun etc are better. If you check the reviews for SendGrid then you will see that it has a lot of negative reviews. I also had major troubles and found the support more than stupid. I’m now using Maileroo.

Additionally, I moved sending emails to a php script. The app sends some json to the script and then the script does some CURL call to have Maileroo send the email. This avoids having the SMTP credentials for Maileroo in the app.

1 Like

Yes, the plan was that the users optionally should be able to enter their own SMTP credentials.
Maileroo looks very interesting, specially their free plan, most of my users will only send out maybe a few hundred emails per month tops, so that would work for them. Otherwise I can probably get by by sending from the main thread.

1 Like