I have 2 different pages that let a user type in their email address to get information generated by the app. I adapted what I am using from a previous post by Wayne. Wayne put the code that opens the port in App.Open. I am putting this code in a WebPage.Open event of a page near the end of the app (just before exiting) because I don’t actually need to send the email until about 30 minutes after the app first starts. I thought there might be a timeout issue if the port is opened too early.
I put this in the Open event of a web page. The page has a property GmailServer which is a SMTPSecureSocket.
GmailServer = new SMTPSecureSocket
GmailServer.Address = "smtp.gmail.com"
GmailServer.Port = 465
GmailServer.Username = "MyGmailUsername"
GmailServer.Password = "MyGmailPassword"
GmailServer.ConnectionType = SMTPSecureSocket.SSLv23
GmailServer.Secure = True
This goes in the code for a button. The msg is an html string:
The problem is that sometimes the message goes through but most of the time it is not getting sent.
Do I need to put all this code in one place (in the button)?
I have tried a bunch of different things but can’t get it to work reliably.
I am not very experienced when it comes to Xojo Web, but i would assume that the GmailServer may be destroyed before the message is sent if the user leaves the Page or closes the Browser before the job is done?
Have you already tried to move the GmailServer into the Session or a Module?
That worked but might not work reliably (that’s the reason I took it out of App.Open to begin with).
Any pros and cons about opening the port in App.Open vs. Session.Open vs. WebPage.Open?
Any security issues? Timing issues?
Also, I can live with it, but if I use Chromebook, email never gets through.
The port isn’t opening until the email is sent. All the first block of code you posted does is create the socket object and assign it certain properties. The actual network communication of opening the port and sending commands doesn’t happen until the .sendmail method is called.
Is it better to put the object in App.Open or Session.Open? You seem to be implying that it doesn’t matter.
Not working on Mac either.
App.Open would be the wrong place. You need to have a separate object for each session. As a rule of thumb, create the object in the scope that you have defined it. So if it’s a property of the page, use WebPage.Open. If it’s a property of the session, then Session.Open. It should not be in App or a Module (because it should not be shared between sessions).
As Sascha noted, the object must live long enough for the mail to be sent, which can take several seconds. Making it a property of the session will give it longer to live and that just might be enough. Otherwise, you have to prevent the user from navigating away from the page if the email is still sending.
[quote=86594:@Tim Hare]As Sascha noted, the object must live long enough for the mail to be sent, which can take several seconds. Making it a property of the session will give it longer to live and that just might be enough. Otherwise, you have to prevent the user from navigating away from the page if the email is still sending.
Not sure if this is will work in a WE app but…
Maybe this is a case for a timer on the server that has a reference to the socket AND to itself. When/if the Send complete event happens, have to the timer Nil it’s reference to itself… If send complete does not happen within a set time have the timer Nil it’s reference to itself.
That way you can control how long the socket lives no matter what the user does but still have things clean up after themselves to avoid a memory leak s, as well as other sessions having access to what is being sent.
Session.Open sounds like the best option for me. I have noticed in netstat it takes a while to open the port in Windows. So, I guess I am going to have to monitor this.
Not sure what is going on in Chromebook and Mac. Firewall perhaps? If so, then this limits the usefulness of this approach.
Do I need to use the DisconnectFromServer when done?
I will look into using SSLConnected, MailSent, and SendProgress to figure out where the problem is.
I think Xojo Cloud might be interfering with making this connection to smtp.gmail.com port 465.
I will start a new thread because this is getting too long plus I have some questions about debugging.
You will definitely have to open up the port before trying to send an email. I’ve been sending to gmail through my web app on Xojo Cloud with no issues.