G
Guest
I have this code to get the messages from a mail server, but whenever I try
to connect it gives me the error "Cast from string " " to type 'Double' is
not valid." Why is it doing this?
Here is a sample of the vars I pass to the connect procedure:
myClient.Connect("mail.server.com", "user_name", "password")
<script runat="server">
Public Class Pop3
Inherits System.Net.Sockets.TcpClient
Public Shared totalBytes As Double = 0
Public Sub New()
End Sub
Public Overloads Sub Connect(ByVal server As String, ByVal username As
String, ByVal password As String)
Dim message As String
Dim sRes As String
MyBase.Connect(server, 110)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception("H1: " & sRes)
End If
message = "USER " & username & Microsoft.VisualBasic.Chr(13) &
Microsoft.VisualBasic.Chr(10)
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception("H2: " & sRes)
End If
message = "PASS " & password & Microsoft.VisualBasic.Chr(13) &
Microsoft.VisualBasic.Chr(10)
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception("H3: " & sRes)
End If
End Sub
Public Sub Disconnect()
Dim message As String
Dim sRes As String
message = "QUIT" & Microsoft.VisualBasic.Chr(13) & "" &
Microsoft.VisualBasic.Chr(10) & ""
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception(sRes)
End If
End Sub
Public Function List() As ArrayList
Dim message As String
Dim sRes As String
Dim retval As ArrayList = New ArrayList()
message = "LIST" & Microsoft.VisualBasic.Chr(13) & "" &
Microsoft.VisualBasic.Chr(10) & ""
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception(sRes)
End If
While True
sRes = GetWebResponse()
If sRes = "." & Microsoft.VisualBasic.Chr(13) & "" &
Microsoft.VisualBasic.Chr(10) & "" Then
Return retval
Else
Dim msg As Pop3Message = New Pop3Message()
Dim seps As Char() = {" "c}
Dim values As String() = sRes.Split(seps)
msg.number = Int32.Parse(values(0))
msg.bytes = Int32.Parse(values(1))
totalBytes += msg.bytes
msg.retrieved = False
retval.Add(msg)
' continue
End If
End While
End Function
Public Function Retrieve(ByVal rhs As Pop3Message) As Pop3Message
Dim message As String
Dim sRes As String
Dim msg As Pop3Message = New Pop3Message()
msg.bytes = rhs.bytes
msg.number = rhs.number
message = "RETR " + rhs.number + "" & Microsoft.VisualBasic.Chr(13)
& "" & Microsoft.VisualBasic.Chr(10) & ""
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception(sRes)
End If
msg.retrieved = True
While True
sRes = GetWebResponse()
If sRes = "." & Microsoft.VisualBasic.Chr(13) & "" &
Microsoft.VisualBasic.Chr(10) & "" Then
Exit While
Else
msg.message += sRes
End If
End While
Return msg
End Function
Public Sub Delete(ByVal rhs As Pop3Message)
Dim message As String
Dim sRes As String
message = "DELE " + rhs.number + "" & Microsoft.VisualBasic.Chr(13)
& "" & Microsoft.VisualBasic.Chr(10) & ""
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception(sRes)
End If
End Sub
Private Sub Write(ByVal message As String)
Dim en As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding()
Dim WriteBuffer(1024) As Byte
WriteBuffer = en.GetBytes(message)
Dim stream As NetworkStream = GetStream()
stream.Write(WriteBuffer, 0, WriteBuffer.Length)
End Sub
Private Function GetWebResponse() As String
Dim enc As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding()
Dim serverbuff(1024) As Byte
Dim stream As NetworkStream = GetStream()
Dim count As Integer = 0
While True
Dim buff(2) As Byte
Dim bytes As Integer = stream.Read(buff, 0, 1)
If bytes = 1 Then
serverbuff(count) = buff(0)
count += 1
If buff(0) = ControlChars.NewLine Then
' break
Exit While
End If
Else
' break
Exit While
End If
End While
Dim retval As String = enc.GetString(serverbuff, 0, count)
Return retval
End Function
Public ReadOnly Property getTotalBytes() As Double
Get
Return totalBytes
End Get
End Property
End Class
Public Class Pop3Exception
Inherits System.ApplicationException
Public Sub New(ByVal str As String)
MyBase.New(str)
End Sub
End Class
Public Class Pop3Message
Public number As Long
Public bytes As Long
Public retrieved As Boolean
Public message As String
End Class
</script>
to connect it gives me the error "Cast from string " " to type 'Double' is
not valid." Why is it doing this?
Here is a sample of the vars I pass to the connect procedure:
myClient.Connect("mail.server.com", "user_name", "password")
<script runat="server">
Public Class Pop3
Inherits System.Net.Sockets.TcpClient
Public Shared totalBytes As Double = 0
Public Sub New()
End Sub
Public Overloads Sub Connect(ByVal server As String, ByVal username As
String, ByVal password As String)
Dim message As String
Dim sRes As String
MyBase.Connect(server, 110)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception("H1: " & sRes)
End If
message = "USER " & username & Microsoft.VisualBasic.Chr(13) &
Microsoft.VisualBasic.Chr(10)
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception("H2: " & sRes)
End If
message = "PASS " & password & Microsoft.VisualBasic.Chr(13) &
Microsoft.VisualBasic.Chr(10)
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception("H3: " & sRes)
End If
End Sub
Public Sub Disconnect()
Dim message As String
Dim sRes As String
message = "QUIT" & Microsoft.VisualBasic.Chr(13) & "" &
Microsoft.VisualBasic.Chr(10) & ""
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception(sRes)
End If
End Sub
Public Function List() As ArrayList
Dim message As String
Dim sRes As String
Dim retval As ArrayList = New ArrayList()
message = "LIST" & Microsoft.VisualBasic.Chr(13) & "" &
Microsoft.VisualBasic.Chr(10) & ""
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception(sRes)
End If
While True
sRes = GetWebResponse()
If sRes = "." & Microsoft.VisualBasic.Chr(13) & "" &
Microsoft.VisualBasic.Chr(10) & "" Then
Return retval
Else
Dim msg As Pop3Message = New Pop3Message()
Dim seps As Char() = {" "c}
Dim values As String() = sRes.Split(seps)
msg.number = Int32.Parse(values(0))
msg.bytes = Int32.Parse(values(1))
totalBytes += msg.bytes
msg.retrieved = False
retval.Add(msg)
' continue
End If
End While
End Function
Public Function Retrieve(ByVal rhs As Pop3Message) As Pop3Message
Dim message As String
Dim sRes As String
Dim msg As Pop3Message = New Pop3Message()
msg.bytes = rhs.bytes
msg.number = rhs.number
message = "RETR " + rhs.number + "" & Microsoft.VisualBasic.Chr(13)
& "" & Microsoft.VisualBasic.Chr(10) & ""
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception(sRes)
End If
msg.retrieved = True
While True
sRes = GetWebResponse()
If sRes = "." & Microsoft.VisualBasic.Chr(13) & "" &
Microsoft.VisualBasic.Chr(10) & "" Then
Exit While
Else
msg.message += sRes
End If
End While
Return msg
End Function
Public Sub Delete(ByVal rhs As Pop3Message)
Dim message As String
Dim sRes As String
message = "DELE " + rhs.number + "" & Microsoft.VisualBasic.Chr(13)
& "" & Microsoft.VisualBasic.Chr(10) & ""
Write(message)
sRes = GetWebResponse()
If Not (sRes.Substring(0, 3) = "+OK") Then
Throw New Pop3Exception(sRes)
End If
End Sub
Private Sub Write(ByVal message As String)
Dim en As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding()
Dim WriteBuffer(1024) As Byte
WriteBuffer = en.GetBytes(message)
Dim stream As NetworkStream = GetStream()
stream.Write(WriteBuffer, 0, WriteBuffer.Length)
End Sub
Private Function GetWebResponse() As String
Dim enc As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding()
Dim serverbuff(1024) As Byte
Dim stream As NetworkStream = GetStream()
Dim count As Integer = 0
While True
Dim buff(2) As Byte
Dim bytes As Integer = stream.Read(buff, 0, 1)
If bytes = 1 Then
serverbuff(count) = buff(0)
count += 1
If buff(0) = ControlChars.NewLine Then
' break
Exit While
End If
Else
' break
Exit While
End If
End While
Dim retval As String = enc.GetString(serverbuff, 0, count)
Return retval
End Function
Public ReadOnly Property getTotalBytes() As Double
Get
Return totalBytes
End Get
End Property
End Class
Public Class Pop3Exception
Inherits System.ApplicationException
Public Sub New(ByVal str As String)
MyBase.New(str)
End Sub
End Class
Public Class Pop3Message
Public number As Long
Public bytes As Long
Public retrieved As Boolean
Public message As String
End Class
</script>