跪求: 用POP3接收邮件的例子(代码)

strXiaoCaiNiao 2004-08-03 05:13:11
怎样用POP3接收邮件呢?请各位大侠不吝赐教!
问题解决后,分不够可以再加!
也可以发到我邮箱里:
lfh187@163.com
lfh187@126.com

小弟在此先谢过各位了!
...全文
720 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
pestd 2005-02-23
  • 打赏
  • 举报
回复
mark先
TonyTonyQ 2004-08-05
  • 打赏
  • 举报
回复
To:楼主
fhlan@163.com邮件的话,要看它的POP3服务器的地址是什么,然后填上就行。但服务器地址是由服务商提供的,不一定就是以“pop3.”开头的,要去看一下邮箱的帮助中心内容。
比如上海热线的pop3服务器是:online.sh.cn,RemoteServer属性填这个就行了。

我帮你看了一下,163的pop3服务器地址:"pop.163.com",填上这个就可以了。不过不知道163
免费邮箱支不支持POP3接收邮件。
你还可以试试其他邮箱,必须支持POP3的邮箱才能用这个类来接收邮件。
你的126的邮箱的pop3地址是:"pop3.126.com"

有问题可联系我: soldierq@msn.com
zytok 2004-08-05
  • 打赏
  • 举报
回复
关注该问题
再帮楼主顶。。。
strXiaoCaiNiao 2004-08-05
  • 打赏
  • 举报
回复
to:qzj(SoldierQ) 先结了,还有200分我另外给你!谢谢各位的帮助!
NEODIO 2004-08-04
  • 打赏
  • 举报
回复
买本Wrox出的《.NET网络高级编程》来看看就明白了
strXiaoCaiNiao 2004-08-04
  • 打赏
  • 举报
回复
谢谢zhanqiangz(闲云野鹤) ,不好意思,是我没有说明白,
我想用System.Net.Sockets在Winform下VB.NET来实现。
zytok 2004-08-04
  • 打赏
  • 举报
回复
ding
strXiaoCaiNiao 2004-08-04
  • 打赏
  • 举报
回复
to:qzj(SoldierQ)
假设我要收:fhlan@163.com的邮件,恕小弟笨得很,请问:
您的pop.RemoteServer = "xxx.mail.com"要怎样设呢?
pop.RemoteServer = "163.mail.com"
pop.RemoteServer = "www.163.com"
pop.RemoteServer = "POP3.163.com"
pop.RemoteServer = "http://www.163.com"
pop.RemoteServer = "fhlan@163.com"
都提示:
Additional information: 找不到这样的主机
strXiaoCaiNiao 2004-08-04
  • 打赏
  • 举报
回复
to:koomis()
打不开呀,是不是要注册会员呀
你能不能帮我拷一下,谢谢
strXiaoCaiNiao 2004-08-04
  • 打赏
  • 举报
回复
to :qzj(SoldierQ)
可用的话,就200分!
谢谢各位的指导!
TonyTonyQ 2004-08-04
  • 打赏
  • 举报
回复
如果楼主喜欢偶的代码的话,给200分哦,呵呵
koomis 2004-08-04
  • 打赏
  • 举报
回复
这很容易的.
你只要弄懂SERVER的通讯就可以了.源代码如下:
http://www.keysheen.com/micheal/ShowPost.asp?id=18
TonyTonyQ 2004-08-04
  • 打赏
  • 举报
回复
最近正好封装了一个POP3类,拿出来献丑了,呵呵,是基于socket的。
注:接收到的邮件如果含附件,需要自己写代码来解码(邮件是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

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)

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))


注:由于刚刚完成这个类,可能在某些方面存在bug,发现的话,请通知我哦。
另:最近还写了一些实用的类,有兴趣请看http://community.csdn.net/Expert/topic/3226/3226208.xml?temp=.2213556
Overriding 2004-08-03
  • 打赏
  • 举报
回复
jmail用pro版,free版不支持pop
Overriding 2004-08-03
  • 打赏
  • 举报
回复
<% @LANGUAGE=VBSCRIPT %>
<%

dim startpage
dim endpage
startpage=int(request("start"))
endpage=int(request("end"))

Set pop3 = Server.CreateObject( "JMail.POP3" )

uid=trim(request("uid"))
pwd=trim(request("pwd"))
pop=trim(request("pop"))

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

'pop3.Connect "agang0084", "921028", "pop3.sina.com.cn"
response.write "<div align=center>"
Response.Write( "您的收件箱有 <font color=red>" & pop3.count & "</font> 封邮件<br><br>" )
response.write "</div>"



'计算页数
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

response.write "当前记录是从第"
response.write startpage
response.write "封到"
response.write endpage
response.write "封的信件"
response.write "<br>"
response.write "<br>"
%>


<html>
<link rel="stylesheet" href="../../css/css.css" type="text/css">


<body bgcolor="eeeeee">
<TABLE width="100%" cellpadding="0" cellspacing="0" border="1" bordercolor="#eeeeee">
<% for i=startpage to endpage %>
<tr>
<td width="11%" bordercolor="#333333">主题:</td>
<td width="77%" bordercolor="#333333"> <%= msg(i).Subject %></td>
<td width="12%" bordercolor="#333333">
<div align="center"><a href="../deletemail.asp?messageid=<%=i%>">删除</a></div>
</td>
</tr>
<tr>
<td width="11%" bordercolor="#333333">发件人:</td>
<td colspan="2" bordercolor="#333333"> <a href="test_SENDMAIL.asp?mailto=<%=msg(i).from%>"><%= msg(i).From %></a>    <font color="#FF0000">点击地址回复信件</font></td>
</tr>
<tr>
<td width="11%" bordercolor="#333333">附件个数:</td>
<td colspan="2" bordercolor="#333333"> <%= Attachments(i).count %>    <font color="#FF0000">接收附件</font></td>
</tr>
<tr>
<td width="11%" bordercolor="#333333">正文:</td>
<td colspan="2" bordercolor="#333333"> <%= msg(i).Body %></td>
</tr>
<tr>
<td colspan="3">    </td>
</tr>
<% next
%>
</TABLE>

</body>
</html>
<%

end if

pop3.Disconnect

%>

zytok 2004-08-03
  • 打赏
  • 举报
回复
Ding

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧