连接到Exchange服务器

时间:2011-01-14 19:44:59

标签: asp.net vb.net exchange-server pop3

我正在使用此代码使用POP3连接到我的Exchange服务器。

我正在尝试检索一个基本上没有被阅读过的电子邮件的收件箱,但我只是得到了一堆胡言乱语和阅读电子邮件。

你能帮我修改我的代码,只是阅读最新的消息。

Try
    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

当我使用top而不是retr时,我得到了这个

+OK X-MimeOLE: Produced By Microsoft Exchange V6.5 Received: by xch0p.com id <01CB58CE.A6A4EA@p.com>; Mon, 20 Sep 2010 09:17:07 -0500 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01CB58CE.81A6A4EA" Content-class: urn:content-classes:message Subject: What we need to do?? Date: Mon, 20 Sep 2010 09:17:07 -0500 Message-ID: <58BFAFC0B9884245A005DFC0C3F73B8604188DF0@abc-exch01.rp.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: What we need to do?? Thread-Index: ActV7LH3Idm5VVuiRXqkVkVTKZy6fgC4Xsmw X-Priority: 1 Priority: Urgent Importance: high Sensitivity: Company-Confidential From: "Mooq" To: "Everyone " . 

2 个答案:

答案 0 :(得分:1)

为什么要自己,图书馆怎么样? http://sourceforge.net/projects/hpop/

答案 1 :(得分:1)

我不知道它是否适合您(必须使用Exchange 2007或2010),但我在最近的一些项目中使用Exchange Web Services并且运行良好。

链接:http://msdn.microsoft.com/en-us/library/bb204119.aspx