Send Email - Confused!

I am creating a web project which runs on a remote server and I need to know when the project starts and stops (for monitoring). I have created a method using the SMTPsocket and all works great on the App Open event but the issue is that when I call the method in the app Close event the app closes just after the sendMail and it would appear that it has closed before the email is sent. I know their MailSent event but as I am creating the socket etc in code I am unsure how I can trap the event and how to tell the app to wait until the event has occurred or a server error has happened.

Since email sending is asynchronous, you’d need to return false in the CancelClose event so that the app won’t actually quit. Only return true once the email has been sent (set a flag so you know when that is true. You may need to start a timer that quits the app for real once the email has been sent.

Hi Marc

I am sorry but I am really struggling with getting my head around how you can create an event handler for something that doesnt exist on a page but has been created in code. In fact I can get my head around creating a timer in code but again I cant understand how you create the handler to trap when the timer event fires. I have looked through the Xojo docs but and still confused.


Look up AddHandler in the Language Reference. You create a method that implements what you want the event to do, eg.

Sub TimerAction(t as timer)
   // code to handle the event
End Sub

When you create the timer, use AddHandler to connect the event to your method.

dim theTimer as New Timer
AddHandler theTimer.Action, AddressOf TimerAction

Thanks guys.

Going right back to the beginning of this conversation, I’m not sure monitoring the status of an app from within the app is the best way to do what you’re trying to do. I run a lot of standalone web servers that are set up as windows services and I monitor these from a remote site with this application a Xojo app running as a service on my local windows server. The link is for the Xojo project source btw.

This app uses a HTTPSocket to connect to my Xojo Web app, checks the status & alerts me if anything is wrong. It also alerts me when the problem has been rectified.

Of course because it’s remote it also checks for connectivity to the remote host, the remote host is powered on etc.

Your monitoring from within assumes the web server is at fault and won’t report on any other issues that may be inhibiting the service from working.

This is a great mechanism Wayne, and thanks for sharing the code, but begs the question, what monitors the monitors? :slight_smile:

I’m only half joking, do you have any mechanism for ensuring that monitoring is functioning as intended, maybe periodic calling home or something?

Thanks Wayne, that is really useful and I will look at using it.

AddHandler is one of the best least-known features of RS/Xojo.

As a matter of fact I have a more sophisticated version that monitors multiple standalone web servers, one of those I stop each day around 1pm (via another Xojo Service) for about 2 mins, so I do get email messages daily for that “test” web server. I also use an email to SMS solution so these alerts arrive on my smartphone.

Ah, good stuff, thanks for the info.