Public Class Pop3
Inherits Sockets.TcpClient
Dim Stream As Sockets.NetworkStream
Dim UsesSSL As Boolean = False
Dim SslStream As Security.SslStream
Dim SslStreamDisposed As Boolean = False
Public LastLineRead As String = vbNullString
Public Overloads Sub Connect(ByVal server As String, _
ByVal username As String, _
ByVal password As String, _
Optional ByVal inport As Integer = 110, _
Optional ByVal usessl As Boolean = False)
If Connected Then disconnect()
UsesSSL = usessl
MyBase.Connect(server, inport)
Stream = MyBase.GetStream()
If UsesSSL Then
SslStream = New Security.SslStream(Stream)
SslStream.AuthenticateAsClient(server)
End If
If Not checkresponse() Then Exit Sub
If CBool(Len(username)) Then
Me.Submit("user" & username & vbCrLf)
If Not checkresponse() Then Exit Sub
End If
If CBool(Len(password)) Then
Me.Submit("pass" & password & vbCrLf)
If Not checkresponse() Then Exit Sub
End If
End Sub
Public Function checkresponse() As Boolean
If Not IsConnected() Then Return False
LastLineRead = Me.Response
If (Left(LastLineRead, 3) <> "+ok") Then
Throw New POP3Exception(LastLineRead)
Return False
End If
Return True
End Function
Public Function IsConnected() As Boolean
If Not Connected Then
Throw New POP3Exception("Not Connected to an Pop3 Server")
Return False
End If
Return True
End Function
Public Function Response(Optional ByVal datasize As Integer = 1) As String
Dim enc As New ASCIIEncoding
Dim serverbufr() As Byte
Dim index As Integer = 0
If datasize > 1 Then
ReDim serverbufr(datasize - 1)
Dim dtsz As Integer = datasize
Dim sz As Integer
Do While index < datasize
If UsesSSL Then
sz = SslStream.Read(serverbufr, index, dtsz)
Else
sz = Stream.Read(serverbufr, index, dtsz)
End If
If sz = 0 Then Return vbNullString
index += sz
dtsz -= sz
Loop
Else
ReDim serverbufr(255)
Do
If UsesSSL Then
serverbufr(index) = CByte(SslStream.ReadByte)
Else
serverbufr(index) = CByte(Stream.ReadByte)
End If
If serverbufr(index) = -1 Then Exit Do
index += 1
If serverbufr(index - 1) = 10 Then Exit Do
If index > UBound(serverbufr) Then
ReDim Preserve serverbufr(index + 256)
End If
Loop
End If
Return enc.GetString(serverbufr, 0, index)
End Function
Public Sub Submit(ByVal message As String)
Dim enc As New ASCIIEncoding
Dim WriteBuffer() As Byte = enc.GetBytes(message)
If UsesSSL Then
SslStream.Write(WriteBuffer, 0, WriteBuffer.Length)
Else
Stream.Write(WriteBuffer, 0, WriteBuffer.Length)
End If
End Sub
Public Sub disconnect()
Me.Submit("QUIT" & vbCrLf)
checkresponse()
If UsesSSL Then
SslStream.Dispose()
SslStreamDisposed = True
End If
End Sub
Public Function statistics() As Integer()
If Not IsConnected() Then Return Nothing
Me.Submit("STAT" & vbCrLf)
LastLineRead = Me.Response
If (Left(LastLineRead, 3) <> "+OK") Then
Throw New POP3Exception(LastLineRead)
Return Nothing
End If
Dim msgInfo() As String = Split(LastLineRead, " "c)
Dim result(1) As Integer
result(0) = Integer.Parse(msgInfo(1))
result(1) = Integer.Parse(msgInfo(2))
Return result
End Function
Public Function List() As ArrayList
If Not IsConnected() Then Return Nothing
Me.Submit("LIST" & vbCrLf)
If Not checkresponse() Then Return Nothing
Dim retval As New ArrayList
Do
Dim response As String = Me.Response
If (response = "." & vbCrLf) Then
Exit Do
End If
Dim msg As New Pop3Message
Dim msgInfo() As String = Split(response, " "c)
msg.number = Integer.Parse(msgInfo(0))
msg.bytes = Integer.Parse(msgInfo(1))
msg.Retrieved = False
retval.Add(msg)
Loop
Return retval
End Function
Public Function GetHeader(ByRef msg As Pop3Message, Optional ByVal BodyLines As Integer = 0) As Pop3Message
If Not IsConnected() Then Return Nothing
Me.Submit("TOP " & msg.number.ToString & " " & BodyLines.ToString & vbCrLf)
If Not checkresponse() Then Return Nothing
msg.Message = vbNullString
Do
Dim response As String = Me.Response
If response = "." & vbCrLf Then
Exit Do
End If
msg.Message &= response
Loop
Return msg
End Function
Public Function Retrieve(ByRef msg As Pop3Message) As Pop3Message
If Not IsConnected() Then Return Nothing
Me.Submit("RETR " & msg.number.ToString & vbCrLf)
If Not checkresponse() Then Return Nothing
msg.Message = Me.Response(msg.bytes)
Do
Dim S As String = Response()
If S = "." & vbCrLf Then
Exit Do
End If
msg.Message &= S
Loop
msg.bytes = Len(msg.Message)
Return msg
End Function
Public Sub Delete(ByVal msgHdr As Pop3Message)
If Not IsConnected() Then Exit Sub
Me.Submit("DELE " & msgHdr.number.ToString & vbCrLf)
checkresponse()
End Sub
Public Sub reset()
If Not IsConnected() Then Exit Sub
Me.Submit("RSET" & vbCrLf)
checkresponse()
End Sub
Public Function noop() As Boolean
If Not IsConnected() Then Return False
Me.Submit("NOOP")
Return checkresponse()
End Function
Protected Overrides Sub Finalize()
If SslStream IsNot Nothing AndAlso Not SslStreamDisposed Then
SslStream.Dispose()
End If
MyBase.Finalize()
End Sub
Public Function ReadPop3(ByVal Server As String, _
ByVal username As String, _
ByVal password As String, _
Optional ByVal Inport As Integer = 110, _
Optional ByVal usessl As Boolean = False) As ArrayList
Try
Dim inmail As New Pop3
inmail.Connect(Server, username, password, Inport, usessl)
Dim stats() As Integer = inmail.statistics()
If stats(0) = 0 Then
Return Nothing
End If
Dim locallist As New ArrayList
For Each msg As Pop3Message In inmail.List
locallist.Add(inmail.Retrieve(msg))
Next
inmail.disconnect()
Return locallist
Catch ex As POP3Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Error Encounted")
Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Error Encounted")
End Try
Return Nothing
End Function
End Class
Public Class Pop3Message
Public MailID As Integer = 0
Public ByteCount As Integer = 0
Public number As Integer = 0
Public bytes As Integer = 0
Public Retrieved As Boolean = False
Public Message As String = vbNullString
Public Overrides Function ToString() As String
Return Message
End Function
End Class
Public Class POP3Exception
Inherits ApplicationException
Public Sub New(ByVal str As String)
MyBase.New(str)
End Sub
End Class
.................................................
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Button1.Enabled = False
Dim emailbag As ArrayList = getmail.ReadPop3("Pop.gmail.com", "snshanjava@gmail.com", "shan1986", 995, True)
If emailbag IsNot Nothing AndAlso emailbag.Count <> 0 Then
For Each msg As Pop3Message In emailbag
Me.TextBox1.Text = msg.Message
MsgBox(msg.Message, MsgBoxStyle.OkOnly, "message #" & msg.MailID.ToString & " of " & emailbag.Count.ToString)
Next
Else
MsgBox("no Email found on server ", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "No email")
End If
Me.Button1.Enabled = True
End Sub
她想要使用pop3从gmail服务器获取邮件,如果发现邮件然后在文本框中显示每个电子邮件内容并在messagebox中显示每条消息但在调试时没有错误有异常 - 类型'测试应用程序'pop3异常的例外被抛出.Myproblem是我在我的gmailaccount中有很多电子邮件,但无法检索这些电子邮件,点击事件将处理其他条件,它显示一个消息框,没有在服务器上找到电子邮件。
有人在这个问题上提供帮助。谢谢提前
答案 0 :(得分:-2)
首先,如果您不屑于阅读David Ross Goben的PDF文档,因为您发布的代码是100%他(除了您已删除的评论),您会注意到GMail,Yahoo和Hotmail等提供商需要SSL。
其次,您可以随时通过他的Gmail地址向David R Goben发送消息(如您在.NetEMail课程中提供的PDF文档中所列),我相信他会很高兴知道您的问题以及您如何知道在没有记下他的情况下发布他的代码。
最后,如果您留下代码注释(正如David R Goben最初的那样),其他成员可以更好地理解代码,而不必考虑它。
回到你的问题。
GMail服务器上的消息有可能已被标记为“已读”,这可能意味着当您执行POP3.list时这些消息不会显示。
最好使用POP3.Statistics查看是否确实要下载消息。
如果POP3.List返回消息列表,您可以根据列表中的值获取消息,因为List保存每条消息的UID。
另一种选择是停止阅读您的邮件并允许您的程序执行POP3.Statistics和/或POP3.List以确定服务器上是否有任何消息。
**下次让人们花时间和精力为他们提供可以用于自己利益的代码。