====完整的类文件==== 'POP3 client class 'Copyright 2004 by Q '2004.8 Imports System.Net Imports System.Net.Sockets Imports System.Text Public Class POP3Client Public Event GotResponse(ByVal ResponseText As String) Public Structure MailInformation Public Index As Int32 Public Title As String Public Size As Int32 'Unit is Byte End Structure
Protected sockPOP3 As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Protected m_remote As IPEndPoint Protected m_user As String Protected m_pass As String Protected m_arrMailList As New ArrayList Protected Const BufferLength = 512
Private bufferReceive(BufferLength) As Byte Private bufferSend() As Byte
Public Property RemoteServer() As String Get Return m_remote.Address.ToString End Get Set(ByVal Value As String) 'Use default POP3 port 110 m_remote = New IPEndPoint(Dns.Resolve(Value).AddressList(0), 110) End Set End Property Public Property UserName() As String Get Return m_user End Get Set(ByVal Value As String) m_user = Value End Set End Property
Public Property Password() As String Get Return m_pass End Get Set(ByVal Value As String) m_pass = Value End Set End Property Public Sub New() 'DO NOTHING End Sub Public Sub New(ByVal UserName As String, ByVal Password As String) m_user = UserName m_pass = Password End Sub Public Sub New(ByVal RemoteServer As String, ByVal UserName As String, ByVal Password As String) m_remote = New IPEndPoint(Dns.Resolve(RemoteServer).AddressList(0), 110) m_user = UserName m_pass = Password End Sub
Public Function Login() As Boolean If Connect() = True Then If CorrectedResponse(SendCommand("USER " & UserName)) = True Then If CorrectedResponse(SendCommand("PASS " & Password)) = True Then Return True Else Return False End If Else Return False End If End If End Function
Public Function GetMailList() As ArrayList Dim strList As String If CorrectedResponse(SendCommand("LIST"), strList) = True Then Dim strItem() As String = strList.Split(vbCrLf) Dim strSubItem() As String Dim mailInfo As MailInformation Dim i As Int32 If strItem.Length > 2 Then For i = 1 To strItem.Length - 3 'Ignore the first and the last two items, they are not items' information strSubItem = strItem(i).Split(" ") mailInfo.Index = strSubItem(0) mailInfo.Size = strSubItem(1) m_arrMailList.Add(mailInfo) Next End If Return m_arrMailList Else Return Nothing End If End Function Public Function RetrieveMail(ByVal Index As Int32) As String GetMailList() 'Get mail information first If Index > m_arrMailList.Count - 1 Then Throw New Exception("Invalid Mail Index") End If
Dim buffer(0) As Byte Dim strContent As New StringBuilder Dim intBuffer As Int16 = 0 Dim mailEntry As MailInformation = m_arrMailList.Item(Index - 1)
bufferSend = Encoding.ASCII.GetBytes("RETR " & Index & vbCrLf) sockPOP3.Send(bufferSend) Do intBuffer += sockPOP3.Receive(buffer, 1, SocketFlags.None) strContent.Append(Encoding.ASCII.GetString(buffer)) Loop While intBuffer < mailEntry.Size + 8 '
Return strContent.ToString.Substring(5, strContent.Length - 8) End Function Public Function DeleteMail(ByVal Index As Int32) As Int16 If CorrectedResponse(SendCommand("DELE " & Index)) = True Then Return 1 Else Return 0 End If End Function
Public Sub Logout() SendCommand("QUIT") sockPOP3.Close() End Sub
Private Function SendCommand(ByVal Command As String) As Byte() Try bufferSend = Encoding.ASCII.GetBytes(Command & vbCrLf) sockPOP3.Send(bufferSend)
Return bufferReceive Catch Throw New Exception("Error In Sending Command To Server") End Try End Function
Private Function CorrectedResponse(ByVal ReceivedBytes() As Byte, Optional ByRef Message As String = "") As Boolean Dim strText As String = Encoding.ASCII.GetString(ReceivedBytes) Message = strText RaiseEvent GotResponse(strText) If strText.StartsWith("+OK") Then Return True Else Return False End If End Function Private Function Connect() As Boolean Try sockPOP3.Connect(m_remote) If sockPOP3.Connected = True Then sockPOP3.Receive(bufferReceive) RaiseEvent GotResponse(Encoding.ASCII.GetString(bufferReceive)) Return True Else Return False End If Catch Throw New Exception("Failed to Connect Host") End Try End Function End Class ====调用方法==== pop.RemoteServer = "xxx.mail.com" pop.UserName = "user" pop.Password = "pass" '以上为设置你的用户信息(必须)
'尝试登陆邮件服务器 If pop.Login() = False Then Exit Sub End If
'显示服务器端邮件列表 Dim a As ArrayList = pop.GetMailList() If Not a Is Nothing Then Dim i As Int32 Dim x As POP3Client.MailInformation For i = 0 To a.Count - 1 x = a.Item(i) txtMsg.AppendText(x.Index & ":" & Math.Round(x.Size / 1024, 2) & "KB" & vbCrLf) Next End If '取得邮件原文件(index为服务器上的邮件索引号,从1开始) MsgBox(pop.RetrieveMail(index))
我初学者。。
老师还没教过能指点下嘛
继续加分。
详细点。。
注:接收到的邮件如果含附件,需要自己写代码来解码(邮件是MIME编码,也就是BASE64码),我的类只负责把邮件原文从服务器上取回来,其他如去邮件标题,提取附件等需在你的程序代码中完成(其实是偶还没得及做,呵呵)。
====完整的类文件====
'POP3 client class
'Copyright 2004 by Q
'2004.8
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class POP3Client
Public Event GotResponse(ByVal ResponseText As String)
Public Structure MailInformation
Public Index As Int32
Public Title As String
Public Size As Int32 'Unit is Byte
End Structure
Protected sockPOP3 As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Protected m_remote As IPEndPoint
Protected m_user As String
Protected m_pass As String
Protected m_arrMailList As New ArrayList
Protected Const BufferLength = 512
Private bufferReceive(BufferLength) As Byte
Private bufferSend() As Byte
Public Property RemoteServer() As String
Get
Return m_remote.Address.ToString
End Get
Set(ByVal Value As String)
'Use default POP3 port 110
m_remote = New IPEndPoint(Dns.Resolve(Value).AddressList(0), 110)
End Set
End Property
Public Property UserName() As String
Get
Return m_user
End Get
Set(ByVal Value As String)
m_user = Value
End Set
End Property
Public Property Password() As String
Get
Return m_pass
End Get
Set(ByVal Value As String)
m_pass = Value
End Set
End Property
Public Sub New()
'DO NOTHING
End Sub
Public Sub New(ByVal UserName As String, ByVal Password As String)
m_user = UserName
m_pass = Password
End Sub
Public Sub New(ByVal RemoteServer As String, ByVal UserName As String, ByVal Password As String)
m_remote = New IPEndPoint(Dns.Resolve(RemoteServer).AddressList(0), 110)
m_user = UserName
m_pass = Password
End Sub
Public Function Login() As Boolean
If Connect() = True Then
If CorrectedResponse(SendCommand("USER " & UserName)) = True Then
If CorrectedResponse(SendCommand("PASS " & Password)) = True Then
Return True
Else
Return False
End If
Else
Return False
End If
End If
End Function
Dim strList As String
If CorrectedResponse(SendCommand("LIST"), strList) = True Then
Dim strItem() As String = strList.Split(vbCrLf)
Dim strSubItem() As String
Dim mailInfo As MailInformation
Dim i As Int32
If strItem.Length > 2 Then
For i = 1 To strItem.Length - 3 'Ignore the first and the last two items, they are not items' information
strSubItem = strItem(i).Split(" ")
mailInfo.Index = strSubItem(0)
mailInfo.Size = strSubItem(1)
m_arrMailList.Add(mailInfo)
Next
End If
Return m_arrMailList
Else
Return Nothing
End If
End Function
Public Function RetrieveMail(ByVal Index As Int32) As String
GetMailList() 'Get mail information first
If Index > m_arrMailList.Count - 1 Then
Throw New Exception("Invalid Mail Index")
End If
Dim buffer(0) As Byte
Dim strContent As New StringBuilder
Dim intBuffer As Int16 = 0
Dim mailEntry As MailInformation = m_arrMailList.Item(Index - 1)
bufferSend = Encoding.ASCII.GetBytes("RETR " & Index & vbCrLf)
sockPOP3.Send(bufferSend)
Do
intBuffer += sockPOP3.Receive(buffer, 1, SocketFlags.None)
strContent.Append(Encoding.ASCII.GetString(buffer))
Loop While intBuffer < mailEntry.Size + 8 '
Return strContent.ToString.Substring(5, strContent.Length - 8)
End Function
Public Function DeleteMail(ByVal Index As Int32) As Int16
If CorrectedResponse(SendCommand("DELE " & Index)) = True Then
Return 1
Else
Return 0
End If
End Function
Public Sub Logout()
SendCommand("QUIT")
sockPOP3.Close()
End Sub
Private Function SendCommand(ByVal Command As String) As Byte()
Try
bufferSend = Encoding.ASCII.GetBytes(Command & vbCrLf)
sockPOP3.Send(bufferSend)
Array.Clear(bufferReceive, 0, BufferLength)
sockPOP3.Receive(bufferReceive)
Return bufferReceive
Catch
Throw New Exception("Error In Sending Command To Server")
End Try
End Function
Private Function CorrectedResponse(ByVal ReceivedBytes() As Byte, Optional ByRef Message As String = "") As Boolean
Dim strText As String = Encoding.ASCII.GetString(ReceivedBytes)
Message = strText
RaiseEvent GotResponse(strText)
If strText.StartsWith("+OK") Then
Return True
Else
Return False
End If
End Function
Private Function Connect() As Boolean
Try
sockPOP3.Connect(m_remote)
If sockPOP3.Connected = True Then
sockPOP3.Receive(bufferReceive)
RaiseEvent GotResponse(Encoding.ASCII.GetString(bufferReceive))
Return True
Else
Return False
End If
Catch
Throw New Exception("Failed to Connect Host")
End Try
End Function
End Class
====调用方法====
pop.RemoteServer = "xxx.mail.com"
pop.UserName = "user"
pop.Password = "pass"
'以上为设置你的用户信息(必须)
'尝试登陆邮件服务器
If pop.Login() = False Then
Exit Sub
End If
'显示服务器端邮件列表
Dim a As ArrayList = pop.GetMailList()
If Not a Is Nothing Then
Dim i As Int32
Dim x As POP3Client.MailInformation
For i = 0 To a.Count - 1
x = a.Item(i)
txtMsg.AppendText(x.Index & ":" & Math.Round(x.Size / 1024, 2) & "KB" & vbCrLf)
Next
End If
'取得邮件原文件(index为服务器上的邮件索引号,从1开始)
MsgBox(pop.RetrieveMail(index))