Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27350

VS 2010 NetworkStream being read too early

$
0
0
Hello,

I am trying to communicate with an email account via POP3. I am trying to utilize the NetworkStream class, and all seems to work well when i use F11 to step through the various lines of code....giving the system ample time to receive data. However, when I run it without any breakpoints it appears that I attempt to read the next line of the network stream too early...thus making all of my future traffic offset.

In particular, it is when i try to use the LIST command via POP3. it must take some time to generate the list and my code attempts to read the line of the stream before there is new data...so i dont get the results from the LIST command until the next command is attempted. Maybe there is some way to use the PEEK method? I am really not quite sure...

Let me show my test code (SEE "CUSTOM" function) to give you an idea...

Code:

Imports System.Net.Security
Imports System.Net.Sockets
Imports System.Text
Imports System.IO

Public Class Pop3Mail
    Dim Read_Stream As StreamReader
    Dim POP3 As New TcpClient
    Dim PopHost As String = "pop.example.com"
    Dim UserName As String = "example@example.com"
    Dim Password As String = "ExamplePW"
    'Dim Server_Response As String
    'Dim response As StreamWriter

    'Variables needed for possible other method
    Dim NetStrm As NetworkStream



    Private Sub Connect()

        Try
            POP3.Connect(PopHost, 110)
            NetStrm = POP3.GetStream
            Read_Stream = New StreamReader(POP3.GetStream)

            MsgBox("Connected: " & Read_Stream.ReadLine)
        Catch ex As Exception
            MsgBox("CONNECT: " & ex.Message)
        End Try
    End Sub

    Private Function EnterCmd(ByVal command As String) As String
        Dim RdLine As String = ""
        Dim ResponseText As String = ""

        Try
            Dim m_buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(command)

            NetStrm.Write(m_buffer, 0, m_buffer.Length)

            Do
                RdLine = Read_Stream.ReadLine & vbNewLine
                ResponseText = ResponseText & RdLine
            Loop While Read_Stream.Peek >= 0







            'Return (Read_Stream.ReadLine)
            Return ResponseText
        Catch ex As Exception
            Return "ERROR!"
        End Try

    End Function

    Public Function CUSTOM() As Boolean
        Try
            Connect()
            MsgBox("USER Command: " & EnterCmd("USER " & UserName & vbCrLf))
            MsgBox("PASS Command: " & EnterCmd("PASS " & Password & vbCrLf))
            MsgBox("STAT Command: " & EnterCmd("STAT" & vbCrLf))
            MsgBox("LIST Command: " & EnterCmd("LIST" & vbCrLf))   
            MsgBox("QUIT Command: " & EnterCmd("QUIT" & vbCrLf))

            POP3.Close()

            Return True
        Catch ex As Exception
            MsgBox("Custom ERR: " & ex.Message)
            Return False
        End Try


    End Function
End Class




Any help is GREATLY appreciated!

Thanks!

Viewing all articles
Browse latest Browse all 27350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>