Close Winsock after errors
Hello all,
I have a VB6 application that sends emails through a winsock.
But sometimes while sending an email, an error occurs.
And then when I exit the aplication, the program unloads all forms but the application is still running. I have to double click in the stop button on VB or control + alt+ delete and kill the process.
What I want was to kill the process through the application
I thougt of something like this : ws.close
But this doesn't work...
I hope somebody can help me
:ehh:
Re: Close Winsock after errors
The Winsock control should not freeze your app.
Are you using the Winsock API?
If not, then you probably have some kind of loop or something still going when trying to close the program.
To end the program you could have something like:
VB Code:
Public Sub EndProgram()
'Stop loops here.
'Stop timers here.
'Destroy objects here.
'Unload all forms.
Dim objForm As Form
For Each objForm In Forms
Unload objForm
Next
Set objForm = Nothing
End Sub
And to use it, you'd type:
VB Code:
Private Sub cmdExit_Click()
EndProgram
End Sub
Re: Close Winsock after errors
Hello DigiRev
The control is Winsock Control 6.0
Yes I also believe that it is in loop because my code is this:
VB Code:
ws.SendData rtxtmail.Text & vbCrLf
nextSend = False
While nextSend = False
DoEvents
Wend
' When the app can't send the email the program don't pass through this loop above
nextSend = False
ws.SendData vbCrLf & "." & vbCrLf
While nextSend = False
DoEvents
Wend
And when this happens the last response I have is:
354 Enter message, ending with "." on a line by itself
Re: Close Winsock after errors
Ok, I'm guessing you're sending mail through an SMTP server?
If that's the case, there is no need to use a loop. You can handle all data on the DataArrival event.
The data you send to the server is determined by the command the server sends you...ie: 220 is greeting, which means you would send a HELO statement after. 250 is a response to the HELO statement, which means you would send a MAIL FROM after, etc.
I've forgotten the commands for the SMTP protocol but there is a lot of info on the net.
This is just an example of what I'm talking about.
VB Code:
Option Explicit
'Steps of the SMTP mail send process.
Private Enum SMTP_STEP
stpConnecting = 0
stpConnected = 1
stpGreeting = 2
stpHELO = 3
stpUsername = 4
stpMAILFROM = 5
stpMAILTO = 6
stpMessage = 7
stpDone = 8
End Enum
'This keeps track of where we are in the send mail process.
Private udtStep As SMTP_STEP
'Connected to server.
Private Sub Winsock1_Connect()
udtStep = stpConnected
End Sub
'Receiving response from server.
'This determines what data we will send next.
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String, strCMD As String
Winsock1.GetData strData, vbString, bytesTotal
'220 - Greeting.
'250 - HELO response
' Send MAIL FROM:
'Get command.
strCMD = Left$(strData, 3)
Select Case strCMD
'Greeting.
'Now we send the HELO.
Case "220"
Winsock1.SendData "HELO " & Winsock1.LocalHostName & "@home.com" & vbCrLf
udtStep = stpGreeting
'Response to our HELO command.
Case "250"
'Send mail from:
Winsock1.SendData "MAIL FROM: " & Winsock1.LocalHostName & "@home.com" & vbCrLf
udtStep = stpMAILFROM
'etc...
End Select
End Sub