Sending Emails in background

Hi All,

Is it possible to send emails in background without displaying a UI. What I would like to do is when the user carry out some specific actions, the program will send a notification email out without user intervention.


Short answer Yes.

I use a SMTPSecuresocket where the instance is a property of the Application. I then send email through Sendgrid from this socket.

The important thing is to realize the mail socket must stay in scope until the email has been sent (which is why I use a property of App). Also don’t sendmail while sending mail, so create a queue of new emails and use a state engine to process them in batches.

Thanks Wayne, but can it be done in Xojo native? I tried successfully using SMTPSocket and EmailMessage class to send out email but the UI must be a WebPage instead of WebDialog. If I can make it to be a WebDialog, I might make the WebDialog invisible to “simulate” background sending (possible or not)?

You can just put a SMTPSocket on a webpage and use it.
Or without GUI just use a subclass.
No need for a dialog at all.

(or you can use our CURL Plugin functions to build and send email.)

Hi Wayne,

If I can afford, why go through a cloud-based email solution? and why choose the smtp API or not the web API?

Thanks Christian, it works. Still trying to get the event handler when the email is successfully sent.

You need to subclass SMTPSocket and add events there.
(or for CURL check result of transfer, e.g. via Event, too)

Chris, yes, I have created a subclass of SMTPSocket, I dynamically create an instance of this subclass to send email in the background. However, emails are successfully sent but event handler MailSent does not trigger.

do you keep a reference around to that class?
Not that it is destroyed too early.

Sorry, I do not quite understand as I am still new to Xojo, sorry

Do you do this?

Dim sock as New mySMTPSubclass

Create a new property(call it ‘sock’ or whatever) that has the super of your subclass and then do this instead:

 sock = New mySMTPSubclass

That should keep a referens around so it does not go out of scope before the email has been sent corectly.

I created a subclass named EmailSubClass with SMTPSocket as Super. There is a method named Initialise to receive email parameters, and a property named Successful as boolean to return the result.

dim e as new EmailSubClass
e.Initialise(txtReceiver.text, txtSubject.text, txtContent.text)
if e.Successful = true then


end if

The email can be sent out, just the event handler MailSent in EmailSubClass do not trigger.

Did you try my suggestion above?

e = New EmailSubClass

“e” is a property with EmailSubClass as its Super.

Yes I did, I added a property to the calling module, but maybe I am not doing it the right way :frowning:

Have looked at \Example Projects\Communication\Internet\EmailExample.xojo_binary_project ?

I notice now you are talking about a web project. Next time, try to post in the Web channel.

The example I linked to is for Desktop, but I used it to understand how SMTPSocket works, and applied it to Web. That should help you implement it.