尝试使用IMAP读取VB.net的第一封电子邮件



我正在尝试使用IMAP 读取VB.net中的第一封电子邮件

这是我的代码:

Imports System.Net.Mail
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.ComponentModel
Imports System.Text
Public Class Algebra
Dim TC As TcpClient
Dim NS As NetworkStream
Dim SW As StreamWriter
Dim SR As StreamReader
Sub GetEmail()
TC = New TcpClient("outlook.office365.com", 993)
NS = TC.GetStream()
SW = New StreamWriter(NS)
SR = New StreamReader(NS)
Dim d(TC.ReceiveBufferSize) As Byte
NS.Read(d, 0, CInt(d.Length))'******Hangs here...
Dim r As String = Encoding.ASCII.GetString(d).TrimEnd()
SW.WriteLine("$ LOGIN me@myschool.school.nz MyPassword")'**Changed for security...
SW.Flush()
NS.Read(d, 0, CInt(d.Length))
r = Encoding.ASCII.GetString(d).TrimEnd()
SW.WriteLine("$ FETCH 0 body[text]") '0 is index...
SW.Flush()
NS.Read(d, 0, CInt(d.Length))
MsgBox(Encoding.ASCII.GetString(d).TrimEnd())
End Sub
End Class

当我运行这个时,它会到达NS。读取语句,花费太长时间进行响应(似乎由于错误而挂起?(,然后简单地返回全零作为响应。有什么想法吗?我想可能是简单的事情吧?

非常感谢。。。

这太令人沮丧了。首先,我需要使用SSL(必须更改代码(。然后它仍然没有起作用。最后发现"\r\n"不适用于VB.net仅c…

以下是我如何最终使其适用于Outlook 365(Mic Exchange(!

Imports System.Net.Mail
Imports System.Net.Security
Imports System.Net.Sockets
Imports System.ComponentModel
Imports System.Text
Public Class MyClass
Dim Tcp As TcpClient
Dim Ssl As SslStream
Sub GetEmail() 'Read email (IMAP) with SSL
Tcp = New TcpClient()
Tcp.Connect("outlook.office365.com", 993)
Ssl = New SslStream(Tcp.GetStream())
Ssl.AuthenticateAsClient("outlook.office365.com")
Dim r As String = Stream()
r = Stream(". LOGIN you@youraddr password")
r = Stream(". SELECT INBOX") 'Need this before doing a FETCH
r = Stream(". SEARCH FROM Iwant@fromthisperson.com") 'enumerates all emails that are from this addr
r = Stream(". FETCH 79 (BODY[TEXT])") 'Where 79 is the email number...
r = Stream() 'Need this to get the OK back...
'See the other IMAP commands available at https://www.skytale.net/blog/archives/23-Manual-IMAP.html
r = Stream(". LOGOUT")
Ssl.Dispose()
Tcp.Dispose()
End Sub
Function Stream(Optional a As String = "")
If a <> "" Then Ssl.Write(Encoding.ASCII.GetBytes(a & vbCrLf))
Dim data(65536) As Byte
Dim bytes As Integer = Ssl.Read(data, 0, data.Length)
Dim r As String = Encoding.ASCII.GetString(data, 0, bytes)
Return Strings.Right(r, Len(r) - 2) 'Removes prefix "* " or ". " - You might want to keep this...
End Function
End Class

这就是你所需要的!我写这篇文章是因为有太多不必要的专有软件试图在这上面赚钱。。。让我知道它是否有用。。。

最新更新