📄 pop3mail.java
字号:
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.util .Vector ;
public class pop3mail
{
static String POP3_Ok_Str = "+Ok ";
static String POP3_Err_Str = "-err ";
public static byte POP3_PORT = 110;
//member;
String m_sHost;
int m_nPort;
String m_sUser;
String m_sPass;
Socket m_socket;
boolean m_bConnected;
Vector m_EmailList;
//func.
public pop3mail(){};
public pop3mail(String sHost,int nPort,String sUsr,String nPass){
SetInfo( sHost, nPort, sUsr, nPass);
};
public void SetInfo(String sHost,int nPort,String sUsr,String nPass){
m_sHost = sHost;
m_nPort = nPort;
m_sUser = sUsr;
m_sPass = nPass;
}
public void SetServerInfo(String sHost,int nPort){
m_sHost = sHost;
m_nPort = nPort;
}
public void SetUserInfo(String sUsr,String nPass){
m_sUser = sUsr;
m_sPass = nPass;
}
public int GetMailNum(){
if(m_EmailList==null)
return 0;
return m_EmailList.size ();
}
public Email GetMailAt(int i){
if(m_EmailList==null)
return null;
return (Email)m_EmailList.elementAt (i);
}
public boolean ConnectToServer(){
if( m_bConnected )
return true;
try{
m_socket= new Socket (m_sHost,m_nPort);
if( GetResult()==false )
return false;
}catch(Exception e){
return false;
}
m_bConnected =true;
return true;
}
public boolean DisConnectToServer(){
if( m_bConnected==false )
return true;
Quit();
try{
m_socket.close ();
}catch(Exception e){
return false;
}
m_bConnected=false;
return true;
}
boolean Quit(){
try{
//send hello string;
StringBuffer sHello = new StringBuffer("QUIT\r\n");
if( SendMsg(sHello.toString().getBytes () )==false )
return false;
if( GetResult()==false )
return false;
m_socket.close ();
}catch(Exception e){
return false;
}
return true;
}
public boolean SendMsg( byte[] sMsg){
if( sMsg.length == 0 )
return false;
try{
if( m_bConnected==false )
return false;
//send hello string;
OutputStream sOut = m_socket.getOutputStream();
sOut.write(sMsg);
sOut.flush ();
}catch(Exception e){
return false;
}
return true;
}
boolean GetResult( ){
String sResponse;
byte[] bRead = new byte[1024];
int nRead = -1;
String sRead ;
try{
InputStream sIn = m_socket.getInputStream();
nRead = sIn.read(bRead);
if( nRead <= 0 )
return false;
}catch(Exception e){
return false;
}
if( MailBuff.FindStrNocase (bRead,"-err",bRead.length ,0) != -1 )
return false;
return true;
}
byte[] GetResultAndStr( ){
String sResponse;
byte[] bRead = new byte[1024];
int nRead = -1;
String sRead ;
try{
InputStream sIn = m_socket.getInputStream();
nRead = sIn.read(bRead);
if( nRead <= 0 )
return null;
}catch(Exception e){
return null;
}
if( MailBuff.FindStrNocase (bRead,"-err",bRead.length ,0) != -1 )
return null;
return bRead;
}
public boolean DeleteMail( int nIndex )
{
if( m_bConnected==false )
return false;
try{
//send hello string;
StringBuffer sHello = new StringBuffer("DELE ");
sHello.append (nIndex);
sHello.append ("\r\n");
if( SendMsg(sHello.toString().getBytes () )==false )
return false;
if( GetResult()==false )
return false;
}catch(Exception e){
return false;
}
return true;
}
public boolean LetMeIn(){
if(m_bConnected==false)
if(ConnectToServer()==false)
return false;
//send user name;
try{
//send hello string;
StringBuffer sHello = new StringBuffer("USER ");
sHello.append (m_sUser);
sHello.append ("\r\n");
if( SendMsg(sHello.toString().getBytes () )==false )
return false;
if( GetResult()==false )
return false;
sHello = new StringBuffer("PASS ");
sHello.append (m_sPass);
sHello.append ("\r\n");
if( SendMsg(sHello.toString().getBytes () )==false )
return false;
if( GetResult()==false )
return false;
}catch(Exception e){
return false;
}
return true;
}
boolean ListMail(){
if(m_bConnected==false)
return false;
byte[] sReturn;
int nMailNum,nMailSize;
try{
//send string;
StringBuffer sHello = new StringBuffer("STAT \r\n");
if( SendMsg(sHello.toString().getBytes () )==false )
return false;
if( (sReturn=GetResultAndStr())==null )
return false;
nMailNum = MailBuff.GetFirstNumber(sReturn,sReturn.length ,0);
if( nMailNum <0 )
return false;
if( m_EmailList==null )
m_EmailList = new Vector (nMailNum,5);
for( int i=0;i<nMailNum;i++ ){
sHello = new StringBuffer("LIST ");
sHello.append (i+1);
sHello.append ("\r\n");
if( SendMsg(sHello.toString().getBytes () )==false )
return false;
if( (sReturn=GetResultAndStr())==null )
return false;
nMailSize = MailBuff.GetSecondNumber(sReturn,sReturn.length,0);
Email EM =new Email();
EM.setSize(nMailSize);
m_EmailList.addElement (EM);
}
}catch(Exception e){
return false;
}
return true;
}
int ReadToBuff( byte[] bRead,int nSize ){
String sResponse;
int nRead = -1;
try{
InputStream sIn = m_socket.getInputStream();
nRead = sIn.read(bRead,0,nSize);
}catch(Exception e){
return -1;
}
return nRead;
}
static int DATABLOCK =65536;
Email GetEmail(int i){
if(m_bConnected==false)
return null;
try{
//send string;
byte[] sRead = new byte[DATABLOCK];
StringBuffer sHello = new StringBuffer("RETR ");
sHello.append (i);
sHello.append ("\r\n");
if( SendMsg(sHello.toString().getBytes () )==false )
return null;
Email EM =GetMailAt(i-1);
if(EM==null)
return null;
MailBuff tMB = new MailBuff (EM.getSize(),DATABLOCK);
int nRead=ReadToBuff(sRead,DATABLOCK);
if(nRead<0)
return null;
if( MailBuff.FindStrNocase (sRead,"-err",sRead.length ,0) != -1 )
return null;
while(nRead>0){
tMB.append (sRead,0,nRead);
if( tMB.FindStr ("\r\n.\r\n",0) != -1 )
break;
nRead=ReadToBuff(sRead,DATABLOCK);
}
EM.SetMailData(tMB);
EM.setSize (tMB.getLength ());
return EM;
}catch(Exception e){
return null;
}
}
public Vector getMails(String sHost,int nPort,String sUsr,String nPass
,boolean bDelete ){
SetInfo( sHost, nPort, sUsr, nPass);
if(LetMeIn()==false)
return null;
if(ListMail()==false)
return null;
Email EM;
Vector EMList=new Vector(5,5);
for(int i=0;i<GetMailNum();i++){
EM = GetEmail(i+1);
EmailTranslator.gb_TranslateHeader(EM);
if( EM!=null )
EMList.addElement (EM);
if(bDelete )
DeleteEmail(i+1);
}
DisConnectToServer();
return EMList;
}
public Vector getMails(boolean bDelete ){
if(LetMeIn()==false)
return null;
if(ListMail()==false)
return null;
Email EM;
Vector EMList=new Vector(5,5);
for(int i=0;i<GetMailNum();i++){
EM = GetEmail(i+1);
EmailTranslator.gb_TranslateHeader(EM);
if(EM!=null)
EMList.addElement (EM);
if(bDelete )
DeleteEmail(i+1);
}
DisConnectToServer();
return EMList;
}
boolean DeleteEmail(int i){
if(m_bConnected==false)
return false;
try{
//send string;
byte[] sRead = new byte[DATABLOCK];
StringBuffer sHello = new StringBuffer("DELE \r\n");
sHello.append (i);
sHello.append ("\r\n");
if( SendMsg(sHello.toString().getBytes () )==false )
return false;
if( GetResult()==false )
return false;
}catch(Exception e){
return false;
}
return true;
}
public static Vector GetMailsStatic(String sHost,int nPort,String sUsr,String nPass
,boolean bDelete )
{
pop3mail pop = new pop3mail();
return pop.getMails( sHost, nPort, sUsr, nPass, bDelete );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -