详图解-用C#从POP3服务器取得邮件(提供源代码下载)
2006-01-18 17:15
671 查看
Download source files - 6 Kb
Download demo project - 4 Kb
Here's the algorithm:
Here's the code for when the Connect Button is clicked:
Here's the code for when the Disconnect Button is clicked:
Here's code when Retrieve Button clicked:
Now, you can set the mail message number and then click Retrieve button. Then you'll get the mail according to the mail number that you wrote on Mail Number Box.
RFC 1725
Download demo project - 4 Kb
Introduction
In this article, I'll show you how to retrieve mail from POP server based on RFC 1725.Algorithm for Retrieving Mail
To retrieve mail from POP server, I just follow rule of RFC 1725. You also can download that paper (RFC 1725).Here's the algorithm:
Client : +OK Server POP Ready!! Client : USER xxx Server : +OK Client : PASS yyy Server : +OK user successfully logged on Client : STAT Server : +OK n m Client : RETR 1 Server : +OK ---{ data }----- Client : QUIT Server : +OK Server POP signing off
Implementation
It's easy to implement an application if we know the algorithm to retrieve mail from a POP server. In this article, I use theTcpClientand
NetworkStreamclasses. Firstly, declare public variables:
public TcpClient Server; public NetworkStream NetStrm; public StreamReader RdStrm; public string Data; public byte[] szData; public string CRLF = "/r/n";
Here's the code for when the Connect Button is clicked:
private void ConnectBtn_Click(object sender, System.EventArgs e) { // change cursor into wait cursor Cursor cr = Cursor.Current; Cursor.Current = Cursors.WaitCursor; // create server POP3 with port 110 Server = new TcpClient(POPServ.Text,110); Status.Items.Clear(); try { // initialization NetStrm = Server.GetStream(); RdStrm= new StreamReader(Server.GetStream()); Status.Items.Add(RdStrm.ReadLine()); // Login Process Data = "USER "+ User.Text+CRLF; szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); NetStrm.Write(szData,0,szData.Length); Status.Items.Add(RdStrm.ReadLine()); Data = "PASS "+ Passw.Text+CRLF; szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); NetStrm.Write(szData,0,szData.Length); Status.Items.Add(RdStrm.ReadLine()); // Send STAT command to get information ie: number of mail and size Data = "STAT"+CRLF; szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); NetStrm.Write(szData,0,szData.Length); Status.Items.Add(RdStrm.ReadLine()); // change enabled - disabled button ConnectBtn.Enabled = false; DisconnectBtn.Enabled = true; RetrieveBtn.Enabled = true; // back to normal cursor Cursor.Current = cr; } catch(InvalidOperationException err) { Status.Items.Add("Error: "+err.ToString()); } }
Here's the code for when the Disconnect Button is clicked:
private void DisconnectBtn_Click(object sender, System.EventArgs e) { // change cursor into wait cursor Cursor cr = Cursor.Current; Cursor.Current = Cursors.WaitCursor; // Send QUIT command to close session from POP server Data = "QUIT"+CRLF; szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); NetStrm.Write(szData,0,szData.Length); Status.Items.Add(RdStrm.ReadLine()); //close connection NetStrm.Close(); RdStrm.Close(); // change enabled - disabled button ConnectBtn.Enabled = true; DisconnectBtn.Enabled = false; RetrieveBtn.Enabled = false; // back to normal cursor Cursor.Current = cr; }
Here's code when Retrieve Button clicked:
private void RetrieveBtn_Click(object sender, System.EventArgs e) { // change cursor into wait cursor Cursor cr = Cursor.Current; Cursor.Current = Cursors.WaitCursor; string szTemp; Message.Clear(); try { // retrieve mail with number mail parameter Data = "RETR "+ Number.Text+CRLF; szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); NetStrm.Write(szData,0,szData.Length); szTemp = RdStrm.ReadLine(); if(szTemp[0]!='-') { while(szTemp!=".") { Message.Text += szTemp; szTemp = RdStrm.ReadLine(); } } else { Status.Items.Add(szTemp); } // back to normal cursor Cursor.Current = cr; } catch(InvalidOperationException err) { Status.Items.Add("Error: "+err.ToString()); } }
Testing
Build and run this project. Set the POP server, user and password. After that you'll get a response message from the POP server (you can see on status box) ie: +OK 2 624 if success or -ERR if fail. The words "+OK 2 624" mean you have two emails and total size 624.Now, you can set the mail message number and then click Retrieve button. Then you'll get the mail according to the mail number that you wrote on Mail Number Box.
Reference
MSDN for .NET frameworkRFC 1725
History
20 Jan 2002 - fixed non-critical GUI problemAbout Agus Kurniawan
He gradueted from Sepuluh Nopember Institute of Technology (ITS) in Department of Electrical Engineering, Indonesia. His programming interest is VC++, C#, VB, VB.NET, .NET, VBScript, Delphi, C++ Builder, Assembly, ASP/ASP.NET, and JSP. He's consultant and architect for DMC Server (www.dmcserver.com). He's currently based in Jakarta, Indonesia. Click here to view Agus Kurniawan's online profile. |
相关文章推荐
- 用C#从POP3服务器取得邮件
- POP3用户无法从Domino POP3服务器下载新的邮件
- C#全局hook实例【提供源代码下载】
- C#:注册机的实现【提供源代码下载】
- python读取pop3服务器邮件并且下载
- python读取pop3服务器邮件并且下载
- 在MFC下实现POST网页表单,下载服务器文件或取得网页源代码
- 发布EntLib.com / YAF 开源论坛 v2.5 (ASP.NET/C#) -- 提供源代码下载!
- C#:一个增强的TcpListener(*)服务器演示程序及源代码下载
- 亮点论坛2.0(C#免费版)源代码正式提供下载 ----asp.net论坛开源代码
- C#:一个增强的TcpListener(*)服务器演示程序及源代码下载
- 发布EntLib.com / YAF 开源论坛 v2.5 (ASP.NET/C#) -- 提供源代码下载!
- c# 写的 功能强大的 FTP服务器 完全兼容 任何 FTP客户端,最兼容 IE.功能强大 防DDOS 等 .目前只提供下载.希望大家一起改善
- C#读取设备信息(源代码下载)
- EntLib.com Blog 开源ASP.NET/C# 博客平台 v2.5 发布(提供源码下载)
- 发布EntLib.com / YAF 开源论坛 V3.0 (ASP.NET/C#) -- 提供代码下载!
- 【转】怎样编程实现SSL连接Gmail的POP3服务器,并查阅邮件
- EntLib.com / YAF 开源论坛 V3.0 (ASP.NET/C#) -- 提供代码下载!
- C# 从服务器下载文件代码
- 《JavaWeb---简单应用---服务器向客户端提供音乐资源(三)》--- 利用struts实现文件下载,下载时改变文件的名称