POP3协议解析,如何自己写服务器? LIST之后连接就断开了?!找了很久不知为什么?
我发现只有在 outlook express 中才OK,
(运行之后 Outlook中有新邮件了)
在 Outlook2007 中就有上面问题
LIST之后就没有反应了
报错是
Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at com.softeem.server.PopOutlookServer.getCommand(PopOutlookServer.java:67)
at com.softeem.server.PopOutlookServer.startServer(PopOutlookServer.java:201)
at com.softeem.server.PopOutlookServer.main(PopOutlookServer.java:231)
=、==================================================================
package com.softeem.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import com.softeem.dao.MailDAO;
import com.softeem.dao.UserDAO;
import com.softeem.dto.TbMail;
import com.softeem.dto.TbUser;
public class PopOutlookServer {
ServerSocket ss;
Socket server;
InputStream is;
OutputStream os;
String sendMan;
String receiveMan;
TbUser user;
List<TbMail> mails;//暂存查询到的邮件
public void initStream(){
try {
// 创建服务器,等待连接
ss = new ServerSocket(110);
server = ss.accept();
System.out.println("----------");
// 获取流
is = server.getInputStream();
os = server.getOutputStream();
} catch (Exception e) {
// TODO: handle exception
}
}
public static void responseClient(OutputStream os, String str) {
PrintWriter pw = new PrintWriter(os);
pw.println(str);
pw.flush();
}
public static String getCommand(InputStream is) {
String str = "";
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
str = br.readLine();
System.out.println("------cmd is " + str);
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public boolean connectServer() {
boolean b = false;
// 第一句应该是由服务器发出
responseClient(os,
"+OK Welcome to coremail Mail Pop3 Server <163com[20050206]>");
return true;
}
public boolean auth() {
responseClient(os, "-Err ");
return true;
}
public void login(String command) {
boolean b=false;
String username = command.split(" ")[1];
responseClient(os, "+OK username has get");
command = getCommand(is);
System.out.println(command); // 期待这里返回 pass password
if (command.startsWith("PASS ")) {
String psw = command.split(" ")[1];
UserDAO userDAO = new UserDAO();
user=userDAO.findByUserName(username);
if(user!=null){
if(user.getPsw().equals(psw)){
b=true;
}
}
}
if(b){
responseClient(os, "+OK 2 messages log ok");
}else{
responseClient(os, "-ERR authorization failed");
}
}
public void getStat(String command) {
MailDAO mailDAO=new MailDAO();
int countMail=mailDAO.countMails(user, 1);
int sumSize=mailDAO.sumSize(user, 1);
responseClient(os, "+OK "+countMail+" "+sumSize);
}
public void getUIDL() {
responseClient(os, "+OK 1 1tbisBsHaEX9byI9EQAAsd ");//邮件的唯一标识符
responseClient(os, ".");
}
public void listMails(String command) {
responseClient(os, "+OK list ");
MailDAO mailDAO=new MailDAO();
mails=mailDAO.findMails(user, 1);
int i=0;
int size=0;
for (TbMail mail : mails) {
i++;
size=mail.getMailSize();
responseClient(os,String.valueOf(i)+" "+size);
}
responseClient(os, ".");// 以一个.结束
}
public void getTop(){
responseClient(os, "+OK get TOP? ");//之后仍然在阻塞中
responseClient(os, ".");// 以一个.结束
}
public void recMail(String command) {
String number= command.split(" ")[1];
TbMail mail=mails.get(0);
responseClient(os, "+OK ");
//返回邮件全部内容 最后以 一个 . 结尾
responseClient(os, "Return-Path: <"+mail.getMailFrom()+">");
responseClient(os, "Delivered-To: "+mail.getSendTo());
responseClient(os, "Received: "+mail.getSendDate());
responseClient(os, "");//空行之后就是邮件开始
responseClient(os, "From: "+mail.getMailFrom());
responseClient(os, "To: "+mail.getSendTo());
responseClient(os, "Date: "+mail.getSendTo());
responseClient(os, "Subject: "+mail.getMailSubject());
//再来一个空行 邮件正文开始
String mainBody=mail.getMainBody();
String[] aText=mainBody.split("\n");
for (String str : aText) {
responseClient(os, str);
}
responseClient(os, ".");
}
public void del(String command){
String number= command.split(" ")[1];
TbMail mail=mails.get(0);
//在数据库中将邮箱 标志位已读....
new MailDAO().merge(user, mail, 2);
responseClient(os, "+OK ");
}
public void startServer() {
boolean b = true;
b = connectServer();
String command="";
if (b) {
while (!(command = getCommand(is)).equals("")) {
if (command.startsWith("AUTH")) {
auth();
} else if (command.startsWith("USER")) {
login(command);
} else if (command.startsWith("STAT")) {
getStat(command);
} else if (command.startsWith("UIDL")) {
getUIDL();
} else if (command.startsWith("LIST")) {
listMails(command);
} else if(command.startsWith("TOP")){
getTop();
} else if (command.startsWith("RETR")) {
recMail(command);
}else if (command.startsWith("DELE")) {
del(command);
}else if (command.startsWith("QUIT")) {
responseClient(os, "+OK");
break;
}
}
}
}
public static void main(String[] args) {
PopOutlookServer server=new PopOutlookServer();
server.initStream();
server.startServer();
}
}