Sending email smtp freezes or gets unknown exception

i am opening a socket to send an email, it has worked before.
when in the xojo editor it freezes, when running standalone i get an unknown exception.

please advise

// set up the socket
MailSocket.Address = Server.Text
MailSocket.Port = Val(Port.text)
MailSocket.ConnectionType = SMTPSecureSocket.TLSv1
MailSocket.SMTPConnectionMode = SMTPSecureSocket.ModeSSLTLS
MailSocket.Secure = True

MailSocket.Username = UserName.Text
MailSocket.Password = Password.Text

// look up DB info
Dim cusName, cusAddress As String
Dim MesBody As String

// LoadCusID(ToEmail.Text, cusName, cusAddress)
cusName = MainWin.CusName.Text
cusAddress = MainWin.CusAddress.Text

// populate the email message
mail.FromAddress = FromEmail.Text
mail.Subject = Subject.Text

MesBody  = Replace(Message.Text, "{NAME}", "Customer")
MesBody = Replace(MesBody, "{SERVICE_ADDRESS}", cusAddress)

mail.BodyPlainText = MesBody
//mail.BodyHTML = htmlFld.Text
mail.Headers.AppendHeader("X-Mailer","Demo SMTP Cullins Service")

// add recipients

// send the email
// MailProgress.Visible = True


Where is your Mail = New EmailMessage ?

If you let the MailSocket get destroyed at method end, the email may not yet be sent since that is asynchronous.

how might i avoid distroying the MailSocket ?

You should declare it as a more global variable than at present. If you just declare it as:

Var MailSocket as SMTPSocket

in your method, it will vanish when the method exits (as do all such locally-declared variables).

Where is this code running?

Hi Tim

the var mail is a global
the mailSocket is also declared as global

MailSocket isn’t “global”. It is public. And seems located embedded in a Window that apparently you abruptly close after a SendMail() that is still going on.

I am creating a new MailSSLSocket so it can be Global.
which interface shall i choose when setting up ? Please see photo …

Please advise

I don’t choose any because I always create sockets in code:

Var mysock as TCPSocket
mysock = new TCPSocket

then I set its parameters.

No need.

Just create a property that won’t go away without your request, of type SMTPSecureSocket, and use it as is. If sub-classing, also no need interfaces, just implement your own events as you wish and it’s done.

ahh works

except i dont know how to create events ?
as example how can i know the mail was sent, then close the window ?

meaby you can put error trap if not error the mesagge was send and you can close your windows