====完整的类文件====
'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))
if uid="" or pwd="" or pop="" then
response.redirect "receivemail.asp"
end if
if uid<>"" and pwd<>"" and pop<>"" then
response.cookies("uid")=trim(request("uid"))
response.cookies("pwd")=trim(request("pwd"))
response.cookies("pop")=trim(request("pop"))
else
uid=request.cookies("uid")
pwd=request.cookies("pwd")
pop=request.cookies("pop")
end if
pop3.Connect uid, pwd,pop
'计算页数
pagenum=10 '一页共存几条记录
if pop3.count<pagenum then
pagecount=1
else
intcount=int(pop3.count/pagenum)
modcount=pop3.count mod pagenum
if modcount>0 then
pagecount=intcount+1 '如果总记录除以一页可存放的记录数有余数则页数必须加以一
else
pagecount=intcount
end if
end if
response.write ("您的邮件共有"&pagecount&"页 ")
'计算页码
page=trim(request("page"))
if page<>"" then
select case page
case "firstpage"
startpage=1
endpage=pagenum
case "lastpage"
startpage=(pagecount-1)*pagenum+1
endpage=pop3.count
case "next"
if endpage+10 > pop3.count then
startpage=(pagecount-1)*pagenum+1
endpage=pop3.count
else
startpage=startpage+10
endpage=endpage+10
end if
case "back"
if startpage=pagenum+1 then
startpage=1
endpage=pagenum
else
startpage=startpage-10
endpage=endpage-10
end if
end select
else
if pop3.count<pagenum then
startpage=1
endpage=pop3.count
else
startpage=1
endpage=pagenum
end if
end if
if pop3.count > 0 then
dim msg(20)
dim Attachments(20)
for i=1 to pop3.count
Set msg(i) = pop3.Messages.item(i)
Set Attachments(i) = msg(i).Attachments
next
%>
<%
if startpage=1 then
response.write "第一页 "
else
response.write "<a href=pop3.asp?page=firstpage&start="&startpage&"&end="&endpage&">第一页</a> "
end if
if endpage=pop3.count then
response.write "下一页 "
else
response.write "<a href=pop3.asp?page=next&start="&startpage&"&end="&endpage&">下一页</a> "
end if
if startpage=1 then
response.write "上一页 "
else
response.write "<a href=pop3.asp?page=back&start="&startpage&"&end="&endpage&">上一页</a> "
end if
if endpage=pop3.count then
response.write "最后一页 "
else
response.write "<a href=pop3.asp?page=lastpage&start="&startpage&"&end="&endpage&">最后一页</a> "
end if