ASP.NET中读取POP3邮件的操作

分类: asp.net   出处:iocblog整理  更新时间:2009-04-09   添加到收藏  

ASP.NET读取POP3邮件的操作

 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 = " ";
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

 

 

[1] [2] 下一页


Tag: 邮件



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。