开发者

help convert pop3 connection to imap

  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, sendB开发者_如何学Goytes.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


A straight port, with no error checking added (though there wasn't any in the original, either):

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)


Your original question with only a code sample uses raw network communication. If you need a an IMAP connection in some capacity, you might consider a .NET IMAP library like those listed here to make your life easier.

However if it's for an academic endeavour or situation where you must write the code yourself then you can still use this answer to download the source code of existing open source IMAP .NET library packages to copy and paste the code you need (depending on the license and purpose).

.NET libraries can be used interchangeably between VB.NET, C# and other languages so you can ignore the language tags for the most part if using a precompiled library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜