2012-02-03 118 views
3

POP服务器允许LIST命令返回邮箱中所有电子邮件的列表。不幸的是,它不会返回所有的电子邮件,它只会返回收件箱中的电子邮件。所以如果一封电子邮件登陆一个垃圾文件夹,它就无法找到它。如何使用POP3从垃圾文件夹下载电子邮件

是否可以使用POP从垃圾文件夹下载电子邮件?

这是当前类(S)我使用:

Option Strict On 
Option Explicit On 
Imports System.Net, System.Text 
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 + 255) 
       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 

    '******************************************************************************* 
    ' Function Name : List 
    ' Purpose  : Get the drop listing from the maildrop 
    '    : 
    ' Returns  : Any Arraylist of POP3Message objects 
    '    : 
    ' Typical telNet I/O: 
    'LIST   (submit) 
    '+OK Mailbox scan listing follows 
    '1 2532   (record index and size in bytes) 
    '2 1610 
    '3 12345 
    '.    (end of records terminator) 
    '******************************************************************************* 
    Public Function List() As ArrayList 
     If Not IsConnected() Then Return Nothing 'exit if not in TRANSACTION mode 

     Me.Submit("LIST" & vbCrLf)       'submit List request 
     If Not CheckResponse() Then Return Nothing 'check for a response, but if an error, return nothing 
     ' 
     'get a list of emails waiting on the server for the authenticated user 
     ' 
     Dim retval As New ArrayList       'set aside message list storage 
     Do 
      Dim response As String = Me.Response   'check response 
      If (response = "." & vbCrLf) Then    'done with list? 
       Exit Do          'yes 
      End If 
      Dim msg As New POP3Message      'establish a new message 
      Dim msgInfo() As String = Split(response, " "c) 'separate by spaces, which divide its fields 
      msg.MailID = Integer.Parse(msgInfo(0))   'get the list item number 
      msg.ByteCount = Integer.Parse(msgInfo(1))   'get the size of the email message 
      msg.Retrieved = False       'indicate its message body is not yet retreived 
      retval.Add(msg)         'add a new entry into the retrieval list 
     Loop 
     Return retval          'return the list 
    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.MailID.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.MailID.ToString & vbCrLf) 
     If Not CheckResponse() Then Return Nothing 
     msg.Message = Me.Response(msg.ByteCount) 
     Do 
      Dim S As String = Response() 
      If S = "." & vbCrLf Then 
       Exit Do 
      End If 
      msg.Message &= S 
     Loop 
     msg.ByteCount = Len(msg.Message) 
     Return msg 
    End Function 

    Public Sub Delete(ByVal msgHdr As POP3Message) 
     If Not IsConnected() Then Exit Sub 
     Me.Submit("DELE " & msgHdr.MailID.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 Not SslStreamDisposed Then 
      SslStream.Dispose() 
     End If 
     MyBase.Finalize() 
    End Sub 
End Class 

Public Class POP3Message 
    Public MailID As Integer = 0 
    Public ByteCount 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 
+0

你已经回答了你的问题。服务器仅传送收件箱中的邮件。你无法从客户端改变它。 – 2012-02-03 17:34:34

+0

POP3没有文件夹的概念。在POP3中只有一个大文件夹。 POP3不指定该文件夹中的内容。服务器似乎被配置为不显示电子邮件给垃圾文件夹中的POP3客户端(f.e。in gmail)。 – 2012-02-03 17:38:20

+0

感谢您的回复,我认为这是不可能的,尽管希望我错了! – Chris 2012-02-03 17:39:03

回答

2

按照该意见,POP3标准只允许从“收件箱”下载。它不是为更先进的设计。

如果邮件服务器支持,理想的解决方案是使用IMAP4。

IMAP4允许您保存,标记,复制和删除邮件,以及允许文件夹和子文件夹,并且它不需要独占访问。

+0

似乎不支持IMAP的唯一大型服务器(或服务)是微软的hotmail.com和outlook.com。 Gmail,Apple,Yahoo,Hushmail等。所有这些都与IMAP协同工作。 – Xeoncross 2013-05-08 16:57:51

相关问题