} private string SendCommand(System.Net.Sockets.NetworkStream netstream , String sToSend) { // send command strings and return the response Byte[] bData=System.Text.Encoding.ASCII.GetBytes(sToSend.ToCharArray()); netstream.Write(bData,0,bData.Length); return(GetResponse(netstream)); } // private string GetResponse(System.Net.Sockets.NetworkStream netstream) { //check if there is a response to get and return it Byte[] bytes=new Byte[tcpC.ReceiveBufferSize]; int ret =netstream.Read(bytes, 0, bytes.Length); // Returns the data received string returndata = System.Text.Encoding.ASCII.GetString(bytes); return returndata; } // private void ReadMail(string host,string user,string pass) { //initialise objects // //open connection to server try { tcpC.Connect(host,110); } catch(Exception ex) { Response.Write("Error connecting to host: " + ex.Message+ " - Please check your details and try again"); Response.End(); } // //get response netstream = tcpC.GetStream(); thisResponse=GetResponse(netstream); // //enter user name thisResponse=SendCommand(netstream,"user " +user+"\r\n"); // //enter password thisResponse=SendCommand(netstream,"pass " + pass+"\r\n"); // // check if logged in ok
if (thisResponse.Substring(0,4)!="-ERR") { this.Response.Write("<font face=courier>Logged in OK <BR>"); } else { this.Response.Write("Error logging in, check your user details and try again<BR>"); Response.Write("<P>" +thisResponse); Response.End(); } thisResponse=SendCommand(netstream,"stat" + "\r\n"); //split string to array; string[] tmpArray; tmpArray=thisResponse.Split(' '); //int thisMess; string numMess=tmpArray[1];
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;namespace _123
{
/// <summary>
/// testEmailREC 的摘要?明。
/// </summary>
public class testEmailREC : System.Web.UI.Page
{
//
System.Net.Sockets.TcpClient tcpC=new System.Net.Sockets.TcpClient();
System.Net.Sockets.NetworkStream netstream;
string thisResponse;
protected System.Web.UI.WebControls.TextBox pass;
protected System.Web.UI.WebControls.TextBox user;
protected System.Web.UI.WebControls.TextBox host;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
// protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此?放置用?代?以初始化?面
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:??用是 ASP.NET Web 窗体??器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// ??器支持所需的方法 - 不要使用代???器修改
/// 此方法的?容。
/// </summary>
private void InitializeComponent()
{
this.Button1.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)
{
this.ReadMail(this.host.Text,this.user.Text,this.pass.Text);
}
private string SendCommand(System.Net.Sockets.NetworkStream netstream , String sToSend)
{
// send command strings and return the response
Byte[] bData=System.Text.Encoding.ASCII.GetBytes(sToSend.ToCharArray());
netstream.Write(bData,0,bData.Length);
return(GetResponse(netstream));
}
//
private string GetResponse(System.Net.Sockets.NetworkStream netstream)
{
//check if there is a response to get and return it
Byte[] bytes=new Byte[tcpC.ReceiveBufferSize];
int ret =netstream.Read(bytes, 0, bytes.Length);
// Returns the data received
string returndata = System.Text.Encoding.ASCII.GetString(bytes);
return returndata; }
//
private void ReadMail(string host,string user,string pass)
{
//initialise objects
//
//open connection to server
try
{
tcpC.Connect(host,110);
}
catch(Exception ex)
{
Response.Write("Error connecting to host: " + ex.Message+ " - Please check your details and try again");
Response.End();
}
//
//get response
netstream = tcpC.GetStream();
thisResponse=GetResponse(netstream);
//
//enter user name
thisResponse=SendCommand(netstream,"user " +user+"\r\n");
//
//enter password
thisResponse=SendCommand(netstream,"pass " + pass+"\r\n");
//
// check if logged in ok
if (thisResponse.Substring(0,4)!="-ERR")
{
this.Response.Write("<font face=courier>Logged in OK <BR>");
}
else
{
this.Response.Write("Error logging in, check your user details and try again<BR>");
Response.Write("<P>" +thisResponse);
Response.End();
} thisResponse=SendCommand(netstream,"stat" + "\r\n"); //split string to array;
string[] tmpArray;
tmpArray=thisResponse.Split(' '); //int thisMess;
string numMess=tmpArray[1];
this.Response.Write("<p><hr>");
thisResponse = "";
//
// if (Int32.Parse(numMess) > 0)
{
this.Response.Write("Messages: " +numMess + "<br/>");
for(int thisMess = 1;thisMess<=Int32.Parse(numMess);thisMess++)
{
thisResponse += (SendCommand(netstream,"top " +thisMess+ " 10" + "\r\n")).Replace("\r\n","<br/>");
}
}
else
{
Response.Write("Messages: None" + "<br/>");
}
//
thisResponse += (SendCommand(netstream,"stat" + "\r\n")).Replace("\r\n","<br/>"); //````````````````````````
thisResponse.Replace("+OK","?");//there was a bug with ?!!
//
tmpArray = thisResponse.Split('?');
//
Response.Write(thisResponse); for(int msg=1;msg<tmpArray.Length-1;msg++)
{ Response.Write("<h3>#" + msg + "</h1>" + tmpArray[msg] + "<p>");
} // close connection
thisResponse=SendCommand(netstream,"QUIT" + "\r\n");
tcpC.Close();
}
}
}