Problem with multipage web app

I see I forgot to mention that MailSocket in this Session Method code that I posted is a subclassed SMTPSocket, with a property called Session (As Session):

Dim s As New MailSocket s.Session = App.SessionWithIdentifier(Self.Identifier) Self.MailSocket1 = s

This makes sure MailSocket1 (a Session Property) is always associated with the Session that created it.

[quote=336789:@Michel Bujardet]You said above you had created a class. Actually no need. Here is what you do :

SMailSocket = New SMTPSocket

[/quote]

Hello, I created a class (MailSocket) to implement the event management on it. Don’t know how to do in the property. There is Ralph’s answer about that, but doesn’t understand at all (I said you: Completly lost, doesn’t have enough level! :frowning: )

For the record, here is my post from 2 days ago, the third one in this thread :


[quote=336789:@Michel Bujardet]Yes, add the SMTPSocket as a property.

mySMTPSocket as SMTPSocket

Initialize it in Session Open as

mySMTPSocket = New SMTPSocket

And then instead of simply calling it like mySMTPSocket, prefix it with Session :

Session.mySMTPSocket

It should be pretty easy to refactor your present code.[/code][/quote]

If i do this, (but with MailSocket instead SMTPSocket, to manage the implemented events)

SMailSocket = New MailSocket

(The class with implemented events), then in (From MailSocket As SMTPSocket):

Sub MailSent() WebPage1.TextFieldName.Text="" WebPage2.TextFieldEmail.Text="" MsgBox("Your request was sent. There is a copy in your mailbox.") End Sub

In WebPage1.TextFieldName.Text="" get a NilObjectException…

[quote=336799:@Ralph Alvy]I see I forgot to mention that MailSocket in this Session Method code that I posted is a subclassed SMTPSocket, with a property called Session (As Session):

Dim s As New MailSocket s.Session = App.SessionWithIdentifier(Self.Identifier) Self.MailSocket1 = s

This makes sure MailSocket1 (a Session Property) is always associated with the Session that created it.[/quote]

Hello Ralph

If i put this in Session.Open (understand you want to say there), for my example:

Dim s As New MailSocket s.Session = App.SessionWithIdentifier(Self.Identifier) Self.SMailSocket = s

And add to MailSocket a property Session as Session

In SMailSocket.MailSent() (SMailSocket As MailSocket) Still got in WebPage1.TextFieldName.Text=“” a NilObjectException…

Thanks!

Are you sure you’re creating the EmailMessage properly? Let’s say your EmailMessage is assigned to variable mail. Perhaps you’re doing something like

Dim mail As EmailMessage

instead of

Dim mail As New EmailMessage

[quote=336809:@Ralph Alvy]Are you sure you’re creating the EmailMessage properly? Let’s say your EmailMessage is assigned to variable mail. Perhaps you’re doing something like

Dim mail As EmailMessage

instead of

Dim mail As New EmailMessage

Well, I was doing (In ButtonSend.Action):

[code]
Sub Action()
Dim mail As emailMessage

// Compose email

mail = New EmailMessage
mail.FromAddress = App.kFrom
mail.Subject = “Your request from Xojo App :-)”
…[/code]

Now commented:

// mail = New EmailMessage

And when press the button now, i get a NilObjectException in:

mail.FromAddress = App.kFrom

Thanks!

Dim mail As EmailMailMessage mail = New EmailMessage

is the same as

Dim mail As New EmaiMessage

So my suggestion didn’t add anything to the conversation.

I give up :stuck_out_tongue:

I assume you have this line in Session.Open:

AddHandler Self.SMailSocket.MailSent, AddressOf Self.SMailSocket_MailSent

Assuming the above, what does the Session.SMailSocket_MailSent method look like?

[quote=336854:@Ralph Alvy]I assume you have this line in Session.Open:

AddHandler Self.SMailSocket.MailSent, AddressOf Self.SMailSocket_MailSent

Assuming the above, what does the Session.SMailSocket_MailSent method look like?[/quote]

Hello

I hadn’t it but I added, created the Method: Session.SMailSocket_MailSent(Sender As MailSocket) (Before the parameter i had an error about delegates, but searched and added it) and contains:

WebPage1.TextFieldName.Text="" WebPage2.TextFieldEmail.Text="" MsgBox("Your request was sent. There is a copy in your mailbox.")

And when app launchs, i get a RuntimeError:

Attempting to add a handler for an event that was already handled.

In:

AddHandler Self.SMailSocket.MailSent, AddressOf Self.SMailSocket_MailSent

My Session.Open event is:

Dim s As New MailSocket s.Session = App.SessionWithIdentifier(Self.Identifier) Self.SMailSocket = s

If remove the Dim, then got a NilObjectException in the AddHandler line

Thanks

This whole conversation seems to be based on trying to help Jose apply a fundamental design issue.

A SMTP server will (if credentials & security settings are correct) accept all emails regardless of delivery, it will then respond to the sender email address if the forwarding of that email can’t be accommodated (NB the response maybe from a downstream server). So waiting for a Mail Sent event is worthless as it will always fire (if as above the credentials etc. are correct).

In my Web Projects I have a multiple user email class that exists as a property of the App class & I instantiate in the App.Open Event handler and then simply append an email as required. This source is freeware so just embed it in your Xojo projects & of course I accept no responsibility for its use.

So Jose I suggest you rethink your design & just send the email, clear the fields and move forward.

[quote=336939:@Wayne Golding]This whole conversation seems to be based on trying to help Jose apply a fundamental design issue.

A SMTP server will (if credentials & security settings are correct) accept all emails regardless of delivery, it will then respond to the sender email address if the forwarding of that email can’t be accommodated (NB the response maybe from a downstream server). So waiting for a Mail Sent event is worthless as it will always fire (if as above the credentials etc. are correct).

In my Web Projects I have a multiple user email class that exists as a property of the App class & I instantiate in the App.Open Event handler and then simply append an email as required. This source is freeware so just embed it in your Xojo projects & of course I accept no responsibility for its use.

So Jose I suggest you rethink your design & just send the email, clear the fields and move forward.[/quote]

Hello Wayne

Thank you very much for your answer and your code. Can you provide a little project using it?

I have no problem sending email, my problem is cleaning the fields in the first webpage (from the second where the SMTPSocket is). It seems the socket must be in Session but unable to work. What i don’t understand is why the problem cleaning the first webpage but not the second. I want to clean the fields only when the mail is sent, to prevent the user send again, and if a problem happens notify it. I really want to understand why can’t clear fields in webpage1 (but yes in webpage2) from the MailSent event but yes from a button.action event in the same webpage2.

Perhaps is something obvious, but not seeing it…

Thanks again

you are almost there. You have the Socket in the Session, so it won’t go out of scope before sending the mail. And you have a refence to the Session in the Socket (which you will need to remove or use a WeakRef instead otherwise the Session cannot end because of circular reference - different issue)

the event is fired by the Socket not by the Session. So there is no context and the runtime is not able to find the WebPage1 you are referring to, thus the NOE. You should be able to restore such an context using the reference to the Session you have saved in the Socket subclass and an WebSessionContext before you refer to the WebPage1.

Jose,

This points to the problem, as Tobias has said. Socket can’t find WebPage1 because it’s not firing in a Session.

You already have this code:

Dim s As New MailSocket s.Session = App.SessionWithIdentifier(Self.Identifier) Self.SMailSocket = s

SMailSocket is now tied to a particular Session, via MailSocket.Session property.

Here’s the way I keep track of a page in a session, customized for what I think you’re doing in SMailSocket_MailSent:

[code]// Handle the WebSessionContext.
Dim sk As MailSocket
sk = Self.SMailSocket // SMailSocket is tied to a particular Session in Session.Open

// Get out if the session is gone.
If sk.Session = Nil Then
MsgBox “Session died.”
Return
End If

// Get the page (WebPage1) from that Session.
// Note that this uses that MailSocket.Session property to find the Session
Dim sc As WebSessionContext
sc = New WebSessionContext(sk.Session)
sc.Session.CurrentPage = New WebPage1

// Do what you want to do here with WebPage1 controls
WebPage1.TextFieldEmail.Text=“”
etc. …[/code]

If that works, the next thing to do is to read up on WeakRef, as also suggested by Tobias.

I also usually put a line of code in there that stops full execution of the referenced page’s Shown event, like

sk.Session.RefreshOnly = 1

where Session has a property called RefreshOnly, and the referenced page Shown event has a Return strategically placed so unnecessary code doesn’t get executed during times like these:

// Snippet in referenced page Shown event If Self.RefreshOnly = 1 Or Session.RefreshOnly = 1 Then Self.RefreshOnly = 0 Session.RefreshOnly = 0 Return End If

[quote=337056:@JosMaraTerryJimnez]Can you provide a little project using it?[/quote]email demo.xojo_binary_project.

Thank you @Tobias Bussmann @Ralph Alvy @Wayne Golding by your last answers, today busy with other thing and was unable to check/review them. I promise try to see later or tomorrow.

Thanks all again!