Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15990

[RESOLVED] [.NET Core 2.1] StreamReader ReadToEnd Hangs

$
0
0
This is one of my few attempts at writing a .NET core app and it is using .NET core 2.1.

I'm writing a program to read emails from a POP3 server that will ultimately populate get the message id, date received, who sent the email, and the message (plain text). This is what I have so far:
Code:

' create a TCP client
Using client = New TcpClient()
    ' connect to the POP3 server
    client.Connect(Server, Port)

    ' get the network stream to send/receive data
    Using pop3Stream = client.GetStream()

        ' create and connect to a SSL stream to send the commands
        Using sslStream = new SslStream(pop3Stream)
            sslStream.AuthenticateAsClient(Server)

            ' get the stream reader to read the results
            Using reader = new StreamReader(sslStream)
                reader.ReadLine()

                ' send the username part of the authentication
                Dim command = $"USER {Username}{Environment.NewLine}"
                Dim buffer = Encoding.ASCII.GetBytes(command.ToCharArray())
                sslStream.Write(buffer, 0, buffer.Length)
                reader.ReadLine()
               
                ' send the password part of the authentication
                command = $"PASS {Password}{Environment.NewLine}"
                buffer = Encoding.ASCII.GetBytes(command.ToCharArray())
                sslStream.Write(buffer, 0, buffer.Length)
                reader.ReadLine()
               
                ' the STAT command displays the number of messages currently in the mailbox and the size (in bytes)
                command = $"STAT{Environment.NewLine}"
                buffer = Encoding.ASCII.GetBytes(command.ToCharArray())
                sslStream.Write(buffer, 0, buffer.Length)
                Dim line = reader.ReadLine()
                ' get just the number of messages, an example of what the line variable would be like is: +OK 1 1322
                ' we need just the middle part
                Dim total = line.Split(" "c, StringSplitOptions.RemoveEmptyEntries)(1)

                ' loop from 1 to the total, if there are 0 messages then the loop will be skipped
                For index = 1 To Convert.ToInt32(total)
                    ' the RETR command will actually get the content of the message
                    ' the last line of the message with be a full stop on a single line followed by a CRLF to indicate the end of the message
                    command = $"RETR {index} {Environment.NewLine}"
                    buffer = Encoding.ASCII.GetBytes(command.ToCharArray())
                    sslStream.Write(buffer, 0, buffer.Length)
                    Dim lines = reader.ReadToEnd().Split(Environment.NewLine).ToList()
                   

                    If (lines.Any(Function(l) l.StartsWith("New Entry:"))) Then
                        Dim messageId = lines.FirstOrDefault(Function(l) l.StartsWith("Message-ID:")).Replace("Message-ID:", String.Empty)
                        Dim dateReceived = lines.FirstOrDefault(Function(l) l.StartsWith("Date:")).Replace("Date:", String.Empty)
                        Dim from = lines.FirstOrDefault(Function(l) l.StartsWith("From:")).Replace("From:", String.Empty)
                       
                        Dim row = New ExcelRow With {
                            .DateReceived = dateReceived,
                            .From = from,
                            .MessageId = messageId
                        }

                        Dim messageStartingIndex = lines.IndexOf("Content-Disposition: inline") + 1
                        Dim messageEndingIndex = lines.IndexOf("Content-Type: text/html; charset=""utf-8""") - 2
                        row.Message = String.Join(Environment.NewLine, lines.Skip(messageStartingIndex).Take(messageEndingIndex - messageStartingIndex))

                        Console.WriteLine(row.Message)
                        Console.WriteLine("{0}********************************{0}", Environment.NewLine)
                    End If
                Next
            End Using
        End Using
    End Using
End Using

The issue that I'm running into is that as soon as I create the lines variable in my For/Next loop, the line hangs and won't go to the next line. In other words, I setup a break point, try to step over, and the next line never breaks. This happens even if I try to read the stream line by line like this:
Code:

Dim lines = New List(Of String)
Dim line = reader.ReadLine()
Do While line <> environment.NewLine
    lines.Add(line)
    line = reader.ReadLine()
Loop

What could be causing this?

Viewing all articles
Browse latest Browse all 15990

Latest Images

Trending Articles



Latest Images

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