Check POP3 mailboxes just to see if there is mail or not

Just need some troubleshooting of code...
it works most of the time, but after used long periods of time, it seems to only report 'no'
substitute name(1), pass(1), and server(1) for real ones.
It's based on some free code that I found.  This is supposed to make status.caption display yes if -any- of the 6 mailboxes has email.
Draw a form with an unenabled timer set for 60000 interval and a winsock control and a label named 'status'.
For VB5

Thanks


Dim StatusSoFar As Boolean
Dim CurrentName As String
Dim CurrentPass As String
Dim OnWhichAccount As Integer

Private Enum POP3States
    POP3_Connect
    POP3_USER
    POP3_PASS
    POP3_STAT
    POP3_RETR
    POP3_DELE
    POP3_QUIT
End Enum

Private m_State As POP3States



Private Sub Form_Load()
Call Timer1_Timer
End Sub

Private Sub status_Click()

End Sub

Private Sub Timer1_Timer()
If StatusSoFar = True Then status.Caption = "yes"
If StatusSoFar = False Then status.Caption = "no"
Timer1.Enabled = False
StatusSoFar = False

Call BasicFirst
CurrentName = "(name1)"
CurrentPass = "(pass1)"
Winsock1.Connect "(server1).com", 110
OnWhichAccount = 1
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Dim strData As String

Static intMessages As Integer 'the number of messages to be loaded
Static intCurrentMessage As Integer 'the counter of loaded messages
Static strBuffer As String 'the buffer of the loading message


Winsock1.GetData strData

If Left$(strData, 1) = "+" Or m_State = POP3_RETR Then
Select Case m_State
        Case POP3_Connect
        intMessages = 0
        m_State = POP3_USER
        Winsock1.SendData "USER " & CurrentName & vbCrLf
    Case POP3_USER
        m_State = POP3_PASS
        Winsock1.SendData "PASS " & CurrentPass & vbCrLf
    Case POP3_PASS
        m_State = POP3_STAT
        Winsock1.SendData "STAT" & vbCrLf
    Case POP3_STAT
         intMessages = CInt(Mid$(strData, 5, _
                      InStr(5, strData, " ") - 5))
        If intMessages > 0 Then
            m_State = POP3_QUIT
            Winsock1.SendData "QUIT" & vbCrLf
            'MsgBox "You DO have mail.", vbInformation
            StatusSoFar = True
            Else
            m_State = POP3_QUIT
            Winsock1.SendData "QUIT" & vbCrLf
            'MsgBox "You have not mail.", vbInformation
        End If

    Case POP3_QUIT
    Winsock1.Close

    DoNext
   
    End Select
Else
    Winsock1.Close
    DoNext
   
'MsgBox "POP3 Error: " & strData, vbExclamation , "POP3 Error"
End If

End Sub



Private Sub DoNext()
If OnWhichAccount = 1 Then
Call BasicFirst
CurrentName = "name2"
CurrentPass = "pass2"
Winsock1.Connect "server2", 110
OnWhichAccount = 2
Exit Sub
End If
If OnWhichAccount = 2 Then
Call BasicFirst
CurrentName = "name3"
CurrentPass = "pass3"
Winsock1.Connect "server3", 110
OnWhichAccount = 3
Exit Sub
End If
If OnWhichAccount = 3 Then
Call BasicFirst
CurrentName = "name4"
CurrentPass = "pass4"
Winsock1.Connect "server4", 110
OnWhichAccount = 4
Exit Sub
End If
If OnWhichAccount = 4 Then
Call BasicFirst
CurrentName = "name5"
CurrentPass = "pass5"
Winsock1.Connect "server5", 110
OnWhichAccount = 5
Exit Sub
End If
If OnWhichAccount = 5 Then
Call BasicFirst
CurrentName = "name6"
CurrentPass = "pass6"
Winsock1.Connect "server6", 110
OnWhichAccount = 6
Timer1.Enabled = True
Exit Sub
End If
End Sub
Sub BasicFirst()
m_State = POP3_Connect
Winsock1.Close
Winsock1.LocalPort = 0
End Sub
LVL 1
eversoslightlyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stefanxCommented:
This is quite a regular question. Basically, there appears to be some or other bug with the Winsock control when opening and closing and re-opening it in quick succession. No-one has ever been able to really give a satisfactory answer, so I don't know why this is so.

However, a workaround is pretty straightforward. Basically, make the Winsock1 control a control array by setting the index property of the control to 0. Next, whenever you wish to start a TCP conversation (by saying Winsock1.Connect), first load a new instance of the control into memory i.e

Load Winsock1(1)
Winsock1(1).Connect

When you are done with the communication, unload this control from memory by saying

Unload Winsock1(1)

Obviosuly, you will need to patch all of you references to Winsock1 to Winsock1(1). It seems like a nasty trick, but it works well.

Hope that helps
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcriderCommented:
stefanx et al,

The problem with the winsock control that stefanx described is the Windows socket control does not have the ability to set the socket option SO_REUSEADDR. This problem is defined by microsoft here:

PRB: Winsock Control Generates Error 10048 - Address in Use
http://support.microsoft.com/support/kb/articles/q173/6/19.asp?LNG=ENG&SA=MSDN 
 

Although the reference is for using local ports, this is also the explaination for what stefanx was talking about... And Yes unloading the winsock control will free up the socket for further connection.



Cheers!
0
stefanxCommented:
Thanks for the info McRider.
In the URL you supplied, Microsoft claims that this is "by design". I can't think why one would want to design like that ;).

Anyway, does anyone know if the Winsock Control shipped with VB6 has the same behaviour? Or has it been fixed perhaps ?

While on the topic, the Winsock Control also exhibits a real pain in the backside behaviour in that it sometimes fires the close event before all data has been received. This happens especially if downloading a fair amount of information, and seems to happen specifically often when using the Winsock control to connect to IIS4 (funnily enough IIS3 is fine!). I know how to work around it and can guess that it has something to do with the receive buffer, but it would be a hell of a lot nicer if the Winsock control just worked properly and did not fire the close event before all data has been received. Anyone know of a "fixed" Winsock control that actually works the way it should ?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.