tcpClient.Connect(hostName, 110)
Dim networkStream As NetworkStream = tcpClient.GetStream()
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
Dim sendBytes As Byte()
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
sendBytes = Encoding.ASCII.GetBytes("User " + userName + vbCrLf)
networkStream.Write(sendBytes, 0, sendBytes.Length)
sTemp = networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
sendBytes = Encoding.ASCII.GetBytes("Pass " + userPassword + vbCrLf)
networkStream.Write(sendBytes, 0, sendBytes.Length)
sTemp = networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
sendBytes = Encoding.ASCII.GetBytes("STAT" + vbCrLf)
networkStream.Write(sendBytes, 0, sendBytes.Length)
sTemp = networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
sendBytes = Encoding.ASCII.GetBytes("RETR " + messageNumber + vbCrLf)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
returnMessage = Encoding.ASCII.GetString(bytes)
EmailContent.Text = returnMessage
sendBytes = Encoding.ASCII.GetBytes("QUIT" + vbCrLf)
networkStream.Write(sendBytes, 0, sendBytes.Length)
tcpClient.Close()
Catch ex As Exception
EmailContent.Text = "Could not retrieve email or your inbox is empty"
End Try
答案 0 :(得分:1)
直接端口,未添加任何错误检查(尽管原始版本中没有任何错误检查):
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
sendBytes = Encoding.ASCII.GetBytes("A001 LOGIN " + userName + " " + userPassword + vbCrLf)
networkStream.Write(sendBytes, 0, sendBytes.Length)
sTemp = networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
sendBytes = Encoding.ASCII.GetBytes("A002 SELECT INBOX" + vbCrLf)
networkStream.Write(sendBytes, 0, sendBytes.Length)
sTemp = networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
sendBytes = Encoding.ASCII.GetBytes("A003 FETCH " + messageNumber + " (BODY.PEEK[])" + vbCrLf)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
returnMessage = Encoding.ASCII.GetString(bytes)
EmailContent.Text = returnMessage
sendBytes = Encoding.ASCII.GetBytes("A004 LOGOUT" + vbCrLf)
networkStream.Write(sendBytes, 0, sendBytes.Length)
答案 1 :(得分:0)
仅使用代码示例的原始问题使用原始网络通信。如果您需要某种容量的IMAP连接,可以考虑使用.NET IMAP库like those listed here来简化您的生活。
但是,如果是学术努力或您必须自己编写代码的情况,那么您仍然可以使用此答案下载现有开源IMAP .NET库包的源代码,以复制和粘贴您需要的代码(取决于关于许可和目的)。
.NET库可以在VB.NET,C#和其他语言之间互换使用,因此如果使用预编译库,您可以忽略大部分语言标记。