动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   .Net → 阅读文章

 ASP.NET读取POP3邮件的操作

作者来源: 
阅读 1716 人次 , 2006-3-29 4:33:00 

namespace pop3client
{
  using system.io ;
  using system.net;
  using system.net.sockets ;
  //please note that all code is copyright 2002 by william j dean
  public class pop3client
  {
  public enum connect_state {disc,authorization,transaction,update};

  public string user;
  public string pwd;
  public string pop;
  public bool error;
  public connect_state state=connect_state.disc ;

  //borrowed from agus kurniawan's article:"retrieve mail from a pop3 server using c#" at http://www.codeproject.com/csharp/popapp.asp
  private tcpclient server;
  private networkstream netstrm;
  private streamreader rdstrm;
  private string data;
  private byte[] szdata;
  private string crlf = "\r\n"; 

  public pop3client()
  {
  //nothing to do..just create to object 
  }

  public pop3client(string pop_server,string user_name,string password)
  {
  //put the specied server (pop_server), user (user_name) and password (password)
  //into the appropriate properties.
  pop=pop_server;
  user=user_name;
  pwd=password;
  }

  #region utility methods, some public, some private
  public string connect (string pop_server)
  {
  pop=pop_server;  //put the specified server into the pop property
  return(connect()); //call the connect method
  }
  public string connect()
  {
  //initialize to the pop server. this code snipped "borrowed"
  //with some modifications...
  //from the article "retrieve mail from a pop3 server using c#" at
  //www.codeproject.com by agus kurniawan
  //http://www.codeproject.com/csharp/popapp.asp

  // create server with port 110
  server = new tcpclient(pop,110);  
 
  try
  {
  // initialization
  netstrm = server.getstream();
  rdstrm= new streamreader(server.getstream());

  //the pop session is now in the authorization state
  state=connect_state.authorization ;
  return(rdstrm.readline ());
  }  
  catch(invalidoperationexception err)
  {
  return("error: "+err.tostring());
  }

  }
  private string disconnect ()
  {
  string temp="disconnected successfully.";
  if(state !=connect_state.disc)
  {

  //close connection
  netstrm.close();
  rdstrm.close();
  state=connect_state.disc ;
  }
  else
  {
  temp="not connected.";
  }
  return(temp);
  }

  private void issue_command(string command)
  {
  //send the command to the pop server. this code snipped "borrowed"
  //with some modifications...
  //from the article "retrieve mail from a pop3 server using c#" at
  //www.codeproject.com by agus kurniawan
  //http://www.codeproject.com/csharp/popapp.asp
  data= command + crlf;
  szdata = system.text.encoding.ascii.getbytes(data.tochararray());
  netstrm.write(szdata,0,szdata.length);

  }
  private string read_single_line_response()
  {
  //read the response of the pop server. this code snipped "borrowed"
  //with some modifications...
  //from the article "retrieve mail from a pop3 server using c#" at
  //www.codeproject.com by agus kurniawan
  //http://www.codeproject.com/csharp/popapp.asp
  string temp;
  try
  {
  temp = rdstrm.readline();
  was_pop_error(temp);  
  return(temp);
  }
  catch(invalidoperationexception err)
  {
  return("error in read_single_line_response(): " + err.tostring ()) ;
  }

  }
  private string read_multi_line_response()
  {
  //read the response of the pop server. this code snipped "borrowed"
  //with some modifications...
  //from the article "retrieve mail from a pop3 server using c#" at
  //www.codeproject.com by agus kurniawan
  //http://www.codeproject.com/csharp/popapp.asp
  string temp="";
  string sztemp;

  try
  {
  sztemp = rdstrm.readline();
  was_pop_error(sztemp);  
  if(!error)
  {
 
  while(sztemp!=".")
  {
  temp += sztemp+crlf;
  sztemp = rdstrm.readline();
  }
  }
  else
  {
  temp=sztemp;
  }
  return(temp);
  }
  catch(invalidoperationexception err)
  {
  return("error in read_multi_line_response(): " + err.tostring ());
  }
  }
  private void was_pop_error(string response)
  {
  //detect if the pop server that issued the response believes that
  //an error has occured.

  if(response.startswith ("-"))
  {
  //if the first character of the response is "-" then the
  //pop server has encountered an error executing the last
  //command send by the client
  error=true;
  }
  else
  {
  //success
  error=false;
  }
  }
  #endregion
  #region pop commands
  public string dele(int msg_number)
  {
  string temp;
 
  if (state != connect_state.transaction )
  {
  //dele is only valid when the pop session is in the transaction state
  temp="connection state not = transaction";
  }
  else
  {
  issue_command("dele " + msg_number.tostring ());
  temp=read_single_line_response();  
  }
  return(temp);
  }

  public string list()
  {
  string temp="";
  if (state != connect_state.transaction )
  {
  //the pop command list is only valid in the transaction state
  temp="connection state not = transaction";
  }
  else
  {
  issue_command ("list");
  temp=read_multi_line_response();
  }
  return(temp);  
  }

  public string list(int msg_number)
  {
  string temp="";

  if (state != connect_state.transaction )
  {
  //the pop command list is only valid in the transaction state
  temp="connection state not = transaction";
  }
  else
  {
  issue_command ("list " + msg_number.tostring ());
  temp=read_single_line_response(); //when the message number is supplied, expect a single line response
  }
  return(temp);

  }

  public string noop()
  {
  string temp;
  if (state != connect_state.transaction )
  {
  //the pop command noop is only valid in the transaction state
  temp="connection state not = transaction";
  }
  else
  {
  issue_command ("noop");
  temp=read_single_line_response();

  }
  return(temp);

  }
  public string pass()
  {
  string temp;
  if (state != connect_state.authorization)
  {
  //the pop command pass is only valid in the authorization state
  temp="connection state not = authorization";
  }
  else
  {
  if (pwd !=null)
  {
  issue_command ("pass " + pwd);
  temp=read_single_line_response();

  if (!error)
  {
  //transition to the transaction state
  state=connect_state.transaction;
  }
  }
  else
  {
  temp="no password set.";
  }
  }
  return(temp);
  }
  public string pass(string password)
  {
  pwd=password;  //put the supplied password into the appropriate property
  return(pass()); //call pass() with no arguement
  }

  public string quit()
  {
  //quit is valid in all pop states

  string temp;
  if (state !=connect_state.disc)
  {
  issue_command ("quit");
  temp=read_single_line_response();
  temp += crlf + disconnect();
 
  }
  else
  {
  temp="not connected.";
  }
  return(temp);

  }
  public string retr (int msg)
  {
  string temp="";
  if (state != connect_state.transaction )
  {
  //the pop command retr is only valid in the transaction state
  temp="connection state not = transaction";
  }
  else
  {
  // retrieve mail with number mail parameter
  issue_command ("retr "+ msg.tostring ());
  temp=read_multi_line_response();
  }
  return(temp);

  }

  public string rset()
  {
  string temp;
  if (state != connect_state.transaction )
  {
  //the pop command stat is only valid in the transaction state
  temp="connection state not = transaction";
  }
  else
  {
  issue_command("rset");
  temp=read_single_line_response();
  }
  return(temp);

  }

  public string stat()
  {
  string temp;
  if (state==connect_state.transaction)
  {
  issue_command("stat");
  temp=read_single_line_response();

  return(temp);
  }
  else

  {
  //the pop command stat is only valid in the transaction state
  return ("connection state not = transaction");
  }
  }  

  public string user()
  {
  string temp;
  if (state != connect_state.authorization)
  {
  //the pop command user is only valid in the authorization state
  temp="connection state not = authorization";
  }
  else
  {
  if (user !=null)
  { 
  issue_command("user "+ user);
  temp=read_single_line_response();
  }
  else
  {  //no user has been specified
  temp="no user specified.";
  }
  }
  return(temp);
  }

  public string user(string user_name)
  {
  user=user_name; //put the user name in the appropriate propertity
  return(user()); //call user with no arguements
  }
  #endregion
  }

}

 本文Tags邮件  C#  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:ASP.NET中的状态管理
· 下一篇:用C#编写发手机中文短信息Windows服务
· 样式对象(Style Object)
· 分页功能
· ASP.NET应用程序
· 用Asp.net实现简单的文字水印
· ValidationSummary Web 控件


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077