一:客户端与POP3邮件服务器建立通信链接,并建立Socket的输入/输出数据流:
cs=new Socket(InetAddress.getByName(host),Integer.parseInt(port));
inData=new BufferedReader(new InputStreamReader(cs.getInputStream()));
outData=new BufferedWriter(new OutputStreamReader(cs.getOutputStream()));
二:处理用户身份验证:
msg=“USER“+user;
if(!POP3Request(msg))
return false;
if(!POP3Response())
return false;
msg=“PASS“+pass;
if(!POP3Request(msg))
return false;
if(!POP3Response())
return false;
三:处理邮件接收事务:
msg=“RETR“+mailNo;
if(!POP3Request(msg))
return false;
response=inData.readLine();
if(!response.startsWith("+OK")){
JOptionPane.showMessageDialog((Component)null,response,“POP3 Error“,JOptionPane.ERROR_MESSAGE);
return false;
}
else{
while(true){
response=inData.readLine();
if(response.trim().equals(““)&& !endofHeader){
endofHeader=true;
continue;
}
if(!endofHeader){
if(response.startsWith(“Date:“)){
date=response.substring(response.index(“:“)+2);
header=header+response+linesep;
}
if(response.startsWith(“From:“)){
from=response.substring(response.index(“:“)+2);
header=header+response+linesep;
}
if(response.startsWith(“Reply-To:“)){
header=header+response+linesep;
}
if(response.startsWith(“Subject:“)){
subject=response.substring(response.index(“:“)+2);
header=header+response+linesep;
}
if(response.startsWith(“To:“)){
if(to.equals(““))
to=response.substring(response.index(“:“)+2);
else
to=to+“,“+response.substring(response.index(“:“)+2);
header=header+response+linesep;
}
if(response.startsWith(“Receipients:“)){
if(to.equals(““))
to=response.substring(response.index(“:“)+2);
else
to=to+“,“+response.substring(response.index(“:“)+2);
header=header+response+linesep;
}
if(response.startsWith(“MIME-Version:“)){
header=header+response+linesep;
}
if(response.startsWith(“Content-Transfer-Encoding:“)){
header=header+response+linesep;
}
if(response.startsWith(“Content-Type:“)){
header=header+response+linesep;
}
if(response.startsWith(“Content-Description:“)){
header=header+response+linesep;
}
if(response.startsWith(“Message-ID:“)){
header=header+response+linesep;
}
}
else{
if(response.startsWith(“.“)){
content=content+response+linesep;
break;
}
content=content+response+linesep;
}
}
}
四:发送QUIT指令至服务器,关闭输入/输出数据流及Socket。
try{
if((outData!=null) && (inData!=null) && (cs!=null)){
msg=“QUIT“;
flag=POP3Request(msg);
flag=POP3Response();
}
if(outData!=null) outData.close();
if(inData!=null) inData.close();
if(cs!=null)cs.close();
}catch(IOException ex){}
System.exit(0);