Socket实现的SMTP邮件发送客户端。

来源:互联网 发布:长沙赢时胜软件怎么样 编辑:程序博客网 时间:2024/04/29 23:29

//Modified by zealvampire 2007-4-4,排版

参考Java Cook book, 添加了BASE64密码验证.

邮件标题,正文可能会乱码. 邮件的头应该会有GBK?=之类的标记, 可以自行解码,如果找不到就肯定默认是GB2312吧。

import java.net.*;
import java.io.*;
import java.util.*;

public class SmtpTalk {
    BufferedReader is;
    PrintStream os;
    
private boolean debug=true;
    
private String host;
    sun.misc.BASE64Encoder enc;
    
private String user;
    
private String psw;

   
    
public static void main(String[] argv){
     
if(argv.length!=2){
      System.out.println(
"Usage:java SmtpTalk host user");
      System.exit(
1);
      }

     
     
     
try{
       SmtpTalk st
=new SmtpTalk(argv[0]);
       System.out.println(
"Smtp Talker ready");
      
       st.converse(
"test@163.net",argv[1],"Test message",
                 
"helo there");
     }
catch(Exception ig){
      System.err.println(ig.getMessage());
      System.exit(
1);
      }

    }

     
 
SmtpTalk(String Server)
{
 host
=Server;
  enc 
= new sun.misc.BASE64Encoder();
  user
=new String("madass");
  psw
=new String ("密码");
 
 
try{
   Socket s
=new Socket(host,25);
   is
=new BufferedReader(new InputStreamReader(s.getInputStream()));
   os
=new PrintStream(s.getOutputStream());
   }

  
catch(NoRouteToHostException e){
   die(
1,"No route to host "+host);
   }

  
catch(ConnectException e){
   die(
1,"Connection Refused by"+host);
   }

  
catch(UnknownHostException e){
   die(
2,"Unknown host "+host);
   }

  
catch(IOException e){
   die(
3,"I/O error setting up socket stream "+e);
   }
   
 }

 
 
 
protected void send_cmd (String cmd,String oprnd){
  send_cmd(cmd
+" "+oprnd);
  }

 
 
protected void send_cmd(String cmd){
  
if(debug)
   System.out.println(
">>>"+cmd);
   os.print(cmd
+" ");
   }

 
 
public void send_text(String text){
  os.print(text
+" ");
  }

  
 
protected boolean expect_reply(String rspNum) /*throw SMTPException*/{
      String s
=null;
      
try{
      s
=is.readLine();
      }
catch(IOException e){
       die(
/*EX_IOERR*/3,"I/o error reading from host"+host+" "+e);
       }

      
if(debug) System.out.println("<<<"+s);
      
return s.startsWith(rspNum+" "); 
           }

          
       
        
protected void die(int ret,String msg)/* throw  SMTPException*/{
         
//throw new SMTPException (ret,msg);
         }
  
            
 
public void converse(String sender,String recipients,String  subject,String body) /*throw SMTPException*/{
  
   
if(!expect_reply("220")) die(4,"did not get SMTP greeting");
  
   send_cmd(
"HELO","test@163.net");
   
if(!expect_reply("250")) die(4,"did not get ack our HELO");
   send_cmd(
"RSET");
   
if(!expect_reply("250")) die (4,"not reset");
  
   send_cmd(
"AUTH LOGIN");
   
if(!expect_reply("334")) die (4,"not reset");
  
   send_cmd(enc.encode(user.getBytes()));
   
if(!expect_reply("334")) die (4,"not reset");
  
  send_cmd(enc.encode(psw.getBytes()));
   
if(!expect_reply("334")) die (4,"not reset");
  
 
  
   send_cmd(
"MAIL","From:<"+sender+">");
   
if(!expect_reply("250")) die (/*EX_PROTOCOL*/4,"did not ack our MAIL command");
  
   
//StringTokenizer st=new StringTokenizer(recipients);
   
//while(st.hasMoreTokens()){
    
//String  r=st.nextToken();
    send_cmd("RCPT","To:<"+recipients+">");
    
if(!expect_reply("250"))
       die(
/*EX_PROTOCOL*/4,"did not ack RECP");
       
//}
      
    send_cmd(
"DATA");
    
if(!expect_reply("354")) die(/*EX_PROTOCOL*/4,"did not want our data");
   
    send_text(
"From: "+sender);
    send_text(
"To: "+recipients);
    send_text(
"Subject: "+subject);
    send_text(
"");
    send_text(body
+" ");
   
    send_cmd(
".");
    
if(!expect_reply("250")) die(/*EX_PROTOCOL*/4,"mail not accepted");
   
    send_cmd(
"QUIT");
    
if(!expect_reply("221")) die(/*EX_PROTOCOL*/4,"Other end not closing down"); 
   
   }

 
 }

原创粉丝点击