Results 1 to 3 of 3

Thread: Please help parsing incoming Winsock data

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    SF
    Posts
    93

    Please help parsing incoming Winsock data

    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

    Code:
    Public prevCmd As String
    Public mailServer As String
    Public username As String
    Public password As String
    Code:
    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

    Code:
    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
    Code:
    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
    Last edited by techsent; Jul 5th, 2008 at 10:08 PM.

  2. #2
    Addicted Member _RaJ_'s Avatar
    Join Date
    Apr 2008
    Location
    India!
    Posts
    198

    Re: Please help parsing incoming Winsock data

    vb Code:
    1. Public Function getData(theData As String, theStart As String, theStop As String, GetMidData As Boolean) As String
    2. Dim IStart As Long, IStop As Long
    3.     If InStr(1, theData, theStart, vbTextCompare) Then
    4.         IStart = InStr(1, theData, theStart, vbTextCompare)
    5.             If InStr(IStart, theData, theStop, vbTextCompare) Then
    6.                 IStop = InStr(IStart, Text1, theStop, vbTextCompare) + Len(theStop)
    7.             Else
    8.                 getData = "ERROR GETTING DATA"
    9.                 Exit Function
    10.             End If
    11.                     If GetMidData = True Then IStart = IStart + Len(theStart)
    12.                     If GetMidData = True Then IStop = IStop - Len(theStop)
    13.                 If IStop > IStart Then
    14.                     getData = Mid(theData, IStart, (IStop - IStart))
    15.                 Else
    16.                     getData = "ERROR GETTING DATA"
    17.                 End If
    18.             Else
    19.         getData = "ERROR GETTING DATA"
    20.     End If
    21. End Function


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

    ●════════════════════════════◄►═════════════════════════●
    ___itš bεttεг tΘ bε απ Θρεπ šiππεг, thαπ α ƒαlšε šαiπt___
    ●════════════════════════════◄►═════════════════════════●
    гαj

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    SF
    Posts
    93

    Re: Please help parsing incoming Winsock data

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width