hi,
I am new to this forum.
Hi all,
I am developing a vb.net desktop application that reads mails from the mailserver using pop and imap .
The application is almost fnished .The problem is the some time when a command is send to retrieve a mail using GetMessage() functions the applications is going to a halt
So I tried to run the the code in debug mode and ran line by line .
What I noticed is in the while loop in GetMessage() function is
While Not tmpString.StartsWith(sPrefix)
tmpString = reader.ReadLine
msg = msg & tmpString & vbCrLf
End While
The reader reads line by line at certain point of time it got hangs
Some times it ill catch an exception and give an error . I am pasting the exception below
"Received an unexpected EOF or 0 bytes from the transport stream"
But some times the applications will hang and go for a halt .This applications has to run 24/7 .So if it hangs in the night or it will be problem .
What I want is if the reader is not able to read from the stream after certain time period . the control has to be removed from the reader and the function has to return a message . I am new to this the network level applications and I am running shot of time
So I am in big trouble . If anybody knows this problem please help me
I have pasted the code briefly so that you can get an idea about the program
Public Sub connect()
Dim objTCP As New TcpClient
Dim sslstream As Net.Security.SslStream
Dim reader As StreamReader = Nothing
Try
objTCP.Connect(IMAPserver, Port)
objTCP.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000) ' 5s timeout
sslstream = New Net.Security.SslStream(objTCP.GetStream())
sslstream.AuthenticateAsClient(IMAPserver) ' authenticate as client GMAIL
AuthStatus = sslstream.IsAuthenticated
reader = New IO.StreamReader(sslstream)
fetch()
End If
objTCP.Close()
reader.Close()
sslstream.Close()
Catch ex As Exception
appendFile(strErrPath, "Exception in IMAPClass Connect " & ex.Message.ToString & " " & Date.Now)
End Try
End Sub
Sub fetchTest(ByVal SslStream As SslStream, ByVal reader As StreamReader)
Dim intMailsRead As Integer = 0
Dim intIndex As Integer = 40
Dim strHeader As String = ""
Dim strBody As String = ""
strHeader = GetMessage("FETCH " & intIndex & " RFC822.HEADER", SslStream, reader)
strBody = GetMessage("FETCH " & intIndex & " BODY[text]", SslStream, reader)
Catch ex As Exception
appendFile(strErrPath, " error in fetch" & ex.Message.ToString() & " " & EmailId & " " & Date.Now)
End Try
End Sub
Function GetMessage(ByVal sCommand As String, ByVal sslstream As SslStream, ByVal reader As StreamReader) As String
Dim tmpString As String = ""
Dim msg As String = ""
Try
sendcmd(sCommand, sslstream)
tmpString = reader.ReadLine()
If tmpString <> "" Then
msg = msg & tmpString & vbCrLf
End If
While Not tmpString.StartsWith(sPrefix)
If reader.EndOfStream = True Then
Exit While
End If
tmpString = reader.ReadLine
msg = msg & tmpString & vbCrLf
End While
If InStr(msg, "BAD Could not parse command") > 0 Then
'MsgBox("BAD GETMESSAGE")
End If
Catch ex As Exception
appendFile(strErrPath, "Error in GetMessage, BAD command : command : " & sCommand & Space(3) * "User: " & UserName & ex.Message & Date.Now)
End Try
Return msg
End Function
Public Sub sendcmd(ByVal strCmd As String, ByVal sslstream As SslStream)
Try
Dim Data As String
Dim SzData() As Byte
Data = GetPrefix() & " " & strCmd & vbCrLf
SzData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray())
sslstream.Write(SzData, 0, SzData.Length)
Catch ex As Exception
appendFile(strErrPath, "Exception in sendcmd " & ex.Message.ToString & Date.Now)
End Try
End Sub
Private Function GetPrefix() As String
iPrefix = iPrefix + 1
sPrefix = iPrefix.ToString
While sPrefix.Length < 3
sPrefix = "0" & sPrefix
End While
sPrefix = "A" & sPrefix
Return sPrefix
End Function
Thanks and regards
I am new to this forum.
Hi all,
I am developing a vb.net desktop application that reads mails from the mailserver using pop and imap .
The application is almost fnished .The problem is the some time when a command is send to retrieve a mail using GetMessage() functions the applications is going to a halt
So I tried to run the the code in debug mode and ran line by line .
What I noticed is in the while loop in GetMessage() function is
While Not tmpString.StartsWith(sPrefix)
tmpString = reader.ReadLine
msg = msg & tmpString & vbCrLf
End While
The reader reads line by line at certain point of time it got hangs
Some times it ill catch an exception and give an error . I am pasting the exception below
"Received an unexpected EOF or 0 bytes from the transport stream"
But some times the applications will hang and go for a halt .This applications has to run 24/7 .So if it hangs in the night or it will be problem .
What I want is if the reader is not able to read from the stream after certain time period . the control has to be removed from the reader and the function has to return a message . I am new to this the network level applications and I am running shot of time
So I am in big trouble . If anybody knows this problem please help me
I have pasted the code briefly so that you can get an idea about the program
Public Sub connect()
Dim objTCP As New TcpClient
Dim sslstream As Net.Security.SslStream
Dim reader As StreamReader = Nothing
Try
objTCP.Connect(IMAPserver, Port)
objTCP.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000) ' 5s timeout
sslstream = New Net.Security.SslStream(objTCP.GetStream())
sslstream.AuthenticateAsClient(IMAPserver) ' authenticate as client GMAIL
AuthStatus = sslstream.IsAuthenticated
reader = New IO.StreamReader(sslstream)
fetch()
End If
objTCP.Close()
reader.Close()
sslstream.Close()
Catch ex As Exception
appendFile(strErrPath, "Exception in IMAPClass Connect " & ex.Message.ToString & " " & Date.Now)
End Try
End Sub
Sub fetchTest(ByVal SslStream As SslStream, ByVal reader As StreamReader)
Dim intMailsRead As Integer = 0
Dim intIndex As Integer = 40
Dim strHeader As String = ""
Dim strBody As String = ""
strHeader = GetMessage("FETCH " & intIndex & " RFC822.HEADER", SslStream, reader)
strBody = GetMessage("FETCH " & intIndex & " BODY[text]", SslStream, reader)
Catch ex As Exception
appendFile(strErrPath, " error in fetch" & ex.Message.ToString() & " " & EmailId & " " & Date.Now)
End Try
End Sub
Function GetMessage(ByVal sCommand As String, ByVal sslstream As SslStream, ByVal reader As StreamReader) As String
Dim tmpString As String = ""
Dim msg As String = ""
Try
sendcmd(sCommand, sslstream)
tmpString = reader.ReadLine()
If tmpString <> "" Then
msg = msg & tmpString & vbCrLf
End If
While Not tmpString.StartsWith(sPrefix)
If reader.EndOfStream = True Then
Exit While
End If
tmpString = reader.ReadLine
msg = msg & tmpString & vbCrLf
End While
If InStr(msg, "BAD Could not parse command") > 0 Then
'MsgBox("BAD GETMESSAGE")
End If
Catch ex As Exception
appendFile(strErrPath, "Error in GetMessage, BAD command : command : " & sCommand & Space(3) * "User: " & UserName & ex.Message & Date.Now)
End Try
Return msg
End Function
Public Sub sendcmd(ByVal strCmd As String, ByVal sslstream As SslStream)
Try
Dim Data As String
Dim SzData() As Byte
Data = GetPrefix() & " " & strCmd & vbCrLf
SzData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray())
sslstream.Write(SzData, 0, SzData.Length)
Catch ex As Exception
appendFile(strErrPath, "Exception in sendcmd " & ex.Message.ToString & Date.Now)
End Try
End Sub
Private Function GetPrefix() As String
iPrefix = iPrefix + 1
sPrefix = iPrefix.ToString
While sPrefix.Length < 3
sPrefix = "0" & sPrefix
End While
sPrefix = "A" & sPrefix
Return sPrefix
End Function
Thanks and regards