Do pop with C#
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace g
{
/// <summary>
/// pop3 的摘要说明。
/// </summary>
public class pop3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.ListBox Status;
protected System.Web.UI.WebControls.TextBox Username;
protected System.Web.UI.WebControls.TextBox Password;
protected System.Web.UI.WebControls.TextBox Message;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Button recv;
protected System.Web.UI.WebControls.TextBox num;
public NetworkStream NetStrm;
public StreamReader RdStrm;
public string Data;
public byte[] szData;
public string CRLF = "\r\n";
public string popmsg(string cmd,string par){
Data = cmd+" "+par+CRLF;
szData = System.Text.Encoding.GetEncoding("GB2312").GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
string r=RdStrm.ReadLine();
if(cmd!="RETR") Status.Items.Add(r);
return r;
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.recv.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
//用110端口新建POP3服务器连接
TcpClient Server = new TcpClient(TextBox1.Text,110);
Message.Text=Base64.Base64Code("数学实验基础");
Status.Items.Clear();
try
{
//初始化
NetStrm = Server.GetStream();
RdStrm= new StreamReader(Server.GetStream());
Status.Items.Add("初始化:"+RdStrm.ReadLine());
//登录服务器过程
popmsg("USER",Username.Text);
popmsg("PASS",Password.Text);
//向服务器发送STAT命令,从而取得邮箱的相关信息:邮件数量和大小
string s=popmsg("STAT","");
int totle=Convert.ToInt32(s.Split(' ')[1]);
/*收信*/
//根据邮件编号从服务器获得相应邮件
string szTemp;
int n=Math.Min(Convert.ToInt32(num.Text),totle);
for(int i=1;i<n;i++)
{
popmsg("RETR",i.ToString());
szTemp = RdStrm.ReadLine();
if(szTemp[0]!='-')
{
//不断地读取邮件内容,只到结束标志:英文句号
while(szTemp!=".")
{
if(szTemp.IndexOf("Subject:")==0){
Message.Text += Base64.Base64Decode(szTemp.Substring(20,szTemp.Length-22))+"\n";
// Message.Text+=szTemp.Substring(9)+"\n";
}
szTemp = RdStrm.ReadLine();
}
}
}
popmsg("QUIT","");
}
catch(InvalidOperationException err){
Status.Items.Add("Error: "+err.ToString());
}
}
}
}
干了一件事:简单抓出邮箱中邮件们的标题。
pop操作参考(with c#):
http://study.99net.net/study/program/net/1085471368.html
pop标准:
http://www.javvin.com/protocol/rfc1939.pdf
Class Base64照抄:
http://dev.csdn.net/article/20/20564.shtm
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace g
{
/// <summary>
/// pop3 的摘要说明。
/// </summary>
public class pop3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.ListBox Status;
protected System.Web.UI.WebControls.TextBox Username;
protected System.Web.UI.WebControls.TextBox Password;
protected System.Web.UI.WebControls.TextBox Message;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Button recv;
protected System.Web.UI.WebControls.TextBox num;
public NetworkStream NetStrm;
public StreamReader RdStrm;
public string Data;
public byte[] szData;
public string CRLF = "\r\n";
public string popmsg(string cmd,string par){
Data = cmd+" "+par+CRLF;
szData = System.Text.Encoding.GetEncoding("GB2312").GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
string r=RdStrm.ReadLine();
if(cmd!="RETR") Status.Items.Add(r);
return r;
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.recv.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
//用110端口新建POP3服务器连接
TcpClient Server = new TcpClient(TextBox1.Text,110);
Message.Text=Base64.Base64Code("数学实验基础");
Status.Items.Clear();
try
{
//初始化
NetStrm = Server.GetStream();
RdStrm= new StreamReader(Server.GetStream());
Status.Items.Add("初始化:"+RdStrm.ReadLine());
//登录服务器过程
popmsg("USER",Username.Text);
popmsg("PASS",Password.Text);
//向服务器发送STAT命令,从而取得邮箱的相关信息:邮件数量和大小
string s=popmsg("STAT","");
int totle=Convert.ToInt32(s.Split(' ')[1]);
/*收信*/
//根据邮件编号从服务器获得相应邮件
string szTemp;
int n=Math.Min(Convert.ToInt32(num.Text),totle);
for(int i=1;i<n;i++)
{
popmsg("RETR",i.ToString());
szTemp = RdStrm.ReadLine();
if(szTemp[0]!='-')
{
//不断地读取邮件内容,只到结束标志:英文句号
while(szTemp!=".")
{
if(szTemp.IndexOf("Subject:")==0){
Message.Text += Base64.Base64Decode(szTemp.Substring(20,szTemp.Length-22))+"\n";
// Message.Text+=szTemp.Substring(9)+"\n";
}
szTemp = RdStrm.ReadLine();
}
}
}
popmsg("QUIT","");
}
catch(InvalidOperationException err){
Status.Items.Add("Error: "+err.ToString());
}
}
}
}
干了一件事:简单抓出邮箱中邮件们的标题。
pop操作参考(with c#):
http://study.99net.net/study/program/net/1085471368.html
pop标准:
http://www.javvin.com/protocol/rfc1939.pdf
Class Base64照抄:
http://dev.csdn.net/article/20/20564.shtm
posted on 2005-08-27 18:25 civ3's .NET studying 阅读(370) 评论(0) 编辑 收藏 举报