PDA

Click to See Complete Forum and Search --> : Please help parsing incoming Winsock data


techsent
Jul 5th, 2008, 04:29 PM
Hey guys,

Im using the Winsock control, the POP3 commands (TOP and DELE), a textbox and a parse message sub. This code adds a Remote Start feature to my program.

Basically, the code...

1. Opens Winsock and logs into a POP3 account.
2. It then gets the subject line of each email and if a subject line word matches the keyword previously entered into the proggy, the Remote Start command then starts.
3. Also, after the keyword (which is "Dad" in this code) is matched, the code then deletes the applicable email from the server.

*The way it is now is that Im using the TOP command to get all the email's subject line. Next, the parse message sub then reads the subject line for all emails simultaneously.


The problem that I need help with is matching up the actual TOP "number" that was sent to retrieve the actual "Keyword" email. Once I have this correct TOP number, I can pass it to the DELE command to delete only the "keyword" email from the server.

Modifying the Parse message sub is where I need help. This sub uses the For/next statement which loops through all emails in just one Data Arrival
pass through and since the TOP for/next is also in the Data Arrival event, it only returns "TOP 1" since the parse message is already done.

So I need the parse message to not loop but return the first emails's subject line which will then match the TOP number. The code then moves to the next email and does the same thing so in the end if the email keyword is Number 7 email then TOP will return 7.

If someone could help me with parse code that would read one email at a time instead of spinning through them all, I can then match up the TOP number correctly.

Here's my existing code below...

Thanks,

techsent

'General Declarations

Public prevCmd As String
Public mailServer As String
Public username As String
Public password As String


Private Sub Form_Load()
prevCmd = ""
'Set your mailsever, username, and password
mailServer = "yourPOP3incomingserver"
username = "yourPOP3username"
password = "yourPOP3password"
'Connect to mail server
Winsock1.Connect mailServer, 110
End Sub


Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim data As String
Dim xstring As String
Dim u As Integer
Dim sstring2 As String
sstring2 = "Dad"


For u = 1 To 10
xstring = "TOP " & (u)

If StrComp(sstring2, Text1.Text, vbTextCompare) = 0 Then
MsgBox xstring
Winsock1.SendData "QUIT" & vbCrLf
DoEvents

Exit For

Else

'Get winsock data and find next command ' to send
Select Case prevCmd
Case "USER"
'Send password
Winsock1.SendData "PASS " & password & vbCrLf
prevCmd = "PASS"
Case "PASS"

Winsock1.SendData xstring & vbCrLf
DoEvents

'prevCmd = "TOP"
'Case "TOP"

'get message
Winsock1.GetData data
DoEvents

'Parse message
parseMSG (data)
DoEvents

Case ""
'Send username.
Winsock1.SendData "USER " & username & vbCrLf
prevCmd = "USER"
End Select
End If
Next u

End Sub

Private Sub parseMSG(data As String)

'Get subject
Dim dataHold As Variant 'Data Holder for Split function. Creates an array of lines
Dim epos As Long 'Ending Position for Mid function
Dim spos As Long 'Starting Position for Mid function
'Split data where there are vbCrLf chara ' cters. Which is at the end of every line '
dataHold = Split(data, vbCrLf)

'Examine line of data
For i = LBound(dataHold) To UBound(dataHold)
If dataHold(i) <> "" Then 'If the line is not blank then
'Selects the beginning of each line to s ' ee what info it contains
Select Case UCase(Left(dataHold(i), InStr(1, dataHold(i), ":")))

Case "SUBJECT:" 'If its the subject
spos = InStr(1, dataHold(i), ":") + 2 'Starting Position for Mid function
epos = Len(dataHold(i)) - 1 'Ending Position for Mid function
Text1.Text = Mid$(dataHold(i), spos, epos - spos + 2) 'Select the middle of the data, not including the beginning tag or the vbCrLf's

End Select

End If

Next i

End Sub

_RaJ_
Jul 28th, 2008, 07:27 AM
Public Function getData(theData As String, theStart As String, theStop As String, GetMidData As Boolean) As String
Dim IStart As Long, IStop As Long
If InStr(1, theData, theStart, vbTextCompare) Then
IStart = InStr(1, theData, theStart, vbTextCompare)
If InStr(IStart, theData, theStop, vbTextCompare) Then
IStop = InStr(IStart, Text1, theStop, vbTextCompare) + Len(theStop)
Else
getData = "ERROR GETTING DATA"
Exit Function
End If
If GetMidData = True Then IStart = IStart + Len(theStart)
If GetMidData = True Then IStop = IStop - Len(theStop)
If IStop > IStart Then
getData = Mid(theData, IStart, (IStop - IStart))
Else
getData = "ERROR GETTING DATA"
End If
Else
getData = "ERROR GETTING DATA"
End If
End Function


i wrote this function 2 parse data........ try it !!

techsent
Jul 29th, 2008, 09:48 PM
Hi Raj,

Thanks for crafting that up. I tried it but it came back with the error...

Compile error:

Argument not optional

Actually, I have my original code working now. I ended up using a listbox to get the number of emails and then compared the text to get the correct #.

Techsent