纯JAVA开发邮件服务器——SMTP

因为开发POP3服务器,时间比较紧,先把SMTP服务器代码放上来,等到POP3服务器做好后,会对此日志进行修改,加进相应注释。知道现在用这个的人越来越少了——开源插件功能都太强大了,但是自己做练习还是比较不错的东西,有在做的同学,希望一起交流下啊

/*导入必要类库*/

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SMTPServer {    //主函数,没什么好说的
 public static void main(String[] args) {
  try {
   ServerSocket ss=new ServerSocket(25);   
   while(true){
    Socket s=ss.accept();
    new SMTPSession(s).start();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}

 

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;

public class SMTPSession extends Thread { 


 public static final String CMD_HELO="HELO";//信号
 public static final String CMD_MAIL="MAIL";//
 public static final String CMD_RCPT="RCPT";
 public static final String CMD_DATA="DATA";
 public static final String CMD_QUIT="QUIT";
 public static final String CMD_RSET="RSET";
 public static final String CMD_NOOP="NOOP";
 private Socket s;
 private BufferedReader br;
 private PrintStream ps;
 
 private String from;
 private String to;
 
 public SMTPSession(Socket s) {
  this.s=s;
 }

 public void run() {
  try {
   br=new BufferedReader(
     new InputStreamReader(s.getInputStream())
   );
   ps=new PrintStream(
     s.getOutputStream()
   );
   doWelcome();
   String line=null;
   line=br.readLine();
   while(line!=null){
    System.out.println(line);
    String command=line.substring(0,4).trim();
    if(command.equalsIgnoreCase(CMD_HELO)){
     doHello();
    }
    else if(command.equalsIgnoreCase(CMD_RSET))
     doRset();
    else if(command.equalsIgnoreCase(CMD_MAIL))
     doMail(line);
    else if(command.equalsIgnoreCase(CMD_RCPT))
     doRcpt(line);
    else if(command.equalsIgnoreCase(CMD_DATA))
     doData();
    else if(command.equalsIgnoreCase(CMD_NOOP))
     doNoop();
    else if(command.equalsIgnoreCase(CMD_QUIT)){
     doQuit();
     break;
    }
    line=br.readLine();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  finally{
   try {
    br.close();
    ps.close();
    s.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 private void doNoop() {
  ps.println("250 OK");
 }

 private void doQuit() {
  ps.println("221 GoodBye");
 }

 private void doData() {
  try {
   ps.println("354  end data with <CR><LF>.</LF></CR>");
   String line=null;
   StringBuffer sb=new StringBuffer();
   while((line=br.readLine())!=null){
    if(line.equals("."))
     break;
    sb.append(line+"\r");    
   }
   sb.deleteCharAt(sb.length()-1);
   String sender=from.substring(0,from.indexOf("@"));
   //将信件拷贝到发件夹
   String path="MailBox/"+sender+"/sender/"+System.currentTimeMillis()+".txt";
   System.out.println(path);
   File file=new File(path);
   if(!file.exists()){
    //创建父级目录
    file.getParentFile().mkdirs();
    file.createNewFile();
   }
   PrintWriter pw=new PrintWriter(file);
   pw.println(sb);
   pw.close();
   //将新建内容拷贝到发件夹
   String receiver=to.substring(0,to.indexOf("@"));
   String to_path="MailBox/"+receiver+"/receiver/"+System.currentTimeMillis()+".txt";
   File to_file=new File(to_path);
   if(!to_file.exists()){
    //创建父级目录
    to_file.getParentFile().mkdirs();
    to_file.createNewFile();
   }
   PrintWriter to_pw=new PrintWriter(to_path);
   BufferedReader in = new BufferedReader(new FileReader(path));
            String str;
            while ((str = in.readLine()) != null) {
             to_pw.println(str);
            }
            to_pw.close();
            in.close();
           
   ps.println("250 OK");
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 private void doRcpt(String command) {
  String strTo="to:";
  int index=command.indexOf(strTo);
  if(index==-1)
   index=command.indexOf(strTo.toUpperCase());
  int start=command.indexOf("<");
  int end=command.indexOf(">");
  if(index>4&&start>0&&end>start){
   to=command.substring(start+1, end);
   ps.println("250  OK");
  }
 }
 
 private void doMail(String command) {
  String strFrom="from:";
  int index=command.indexOf(strFrom);
  if(index==-1)
   index=command.indexOf(strFrom.toUpperCase());
  int start=command.indexOf("<");
  int end=command.indexOf(">");
  if(index>4&&start>0&&end>start){
   from=command.substring(start+1, end);
   ps.println("250  OK");
  }
  else{
   ps.println("500  ERROR");
  }
 }

 private void doRset() {
  ps.println("250 OK");
 }

 private void doHello() {
  ps.println("250 OK");
 }

 private void doWelcome() {
  ps.println("220 wrfei");
 }

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值