下面是我的程序,MailMessage类就是一个java bean,内容我就不贴出来了
出错的地方我在程序中标出了,高手们帮忙看看啊package mail;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.StringTokenizer;public class SMTPClient { /**
 * @param args
 * @throws IOException 
 * @throws UnknownHostException 
 */
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
MailMessage message=new MailMessage();
message.setFrom("[email protected]");
message.setTo("[email protected]");
String server="smtp.eyou.com";
message.setSubject("test");
message.setContent("test");
message.setDatafrom("wasingmon");
message.setDatato("wxm");
SMTPClient smtp=new SMTPClient(server,25);
boolean flag;
flag=smtp.sendMail(message,server);
if(flag){
System.out.println("邮件发送成功!");
}
else{
System.out.println("邮件发送失败!");
} }
private Socket socket;
public SMTPClient(String server,int port) throws UnknownHostException, IOException{
try{
socket=new Socket(server,25);
}catch(SocketException e){
System.out.println(e.getMessage());
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("已经建立连接!");
} }

public void helo(String server,BufferedReader in,BufferedWriter out) throws IOException{
int result;
result=getResult(in);
if(result!=220){
throw new IOException("连接服务器失败");
}
result=sendServer("HELO "+server,in,out);
if(result!=250)
{
throw new IOException("注册邮件服务器失败!");
}
}

private int sendServer(String str,BufferedReader in,BufferedWriter out) throws IOException{
out.write(str);
out.newLine();
out.flush();
return getResult(in);
}
public int getResult(BufferedReader in){
String line="";
try{
line=in.readLine();
}catch(Exception e){
e.printStackTrace();
}
//从服务器返回消息中读出状态码,将其转换成整数返回
StringTokenizer st=new StringTokenizer(line," ");
return Integer.parseInt(st.nextToken());
}

public void mailfrom(String source,BufferedReader in,BufferedWriter out) throws IOException{
int result;
result=sendServer("MAIL FROM:<"+source+">",in,out);
if(result!=250){
throw new IOException("指定源地址错误");
}
}

/////////////
/////////////
/////////现在错误出在rcpt()这里,result是从服务器端返回的状态码,这里是设置收件人的地址,
///////////成功的话应该返回250,但是我跟踪了一下,返回的是553,后面还有一点提示消
////////息"553 host denies relay (eyou mta)"
////////这个什么意思?
//////////////
/////////////
public void rcpt(String touchman,BufferedReader in,BufferedWriter out) throws IOException{
int result;
result=sendServer("RCPT TO:<"+touchman+">",in,out);
if(result!=250){
throw new IOException("指定目的地址错误!");
}
}

public void data(String from,String to,String subject,String content,BufferedReader in,BufferedWriter out) throws IOException{
int result;
result=sendServer("DATA",in,out);
if(result!=354){
throw new IOException("不能发送数据");
}
out.write("From:"+from);
out.newLine();
out.write("To:"+to);
out.write("subject:"+subject);
out.newLine();
out.write(content);
out.newLine();
result=sendServer(".",in,out);
System.out.println(result);
if(result!=250)
{
throw new IOException("发送数据错误");
}
}

public void quit(BufferedReader in,BufferedWriter out) throws IOException{
int result;
result=sendServer("QUIT",in,out);
if(result!=221){
throw new IOException("未能正确退出");
}
}
public boolean sendMail(MailMessage message,String server){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
helo(server,in,out);
mailfrom(message.getFrom(),in,out);
rcpt(message.getTo(),in,out);
data(message.getDatafrom(),message.getDatato(),message.getSubject(),message.getContent(),in,out);
System.out.println("dfa7");
quit(in,out);
System.out.println("dfa8");
}catch(Exception e){
e.printStackTrace();
return false;

}
return true;
}}

解决方案 »

  1.   

    //现在的SMTP服务器一般需要验证登陆, 好像你的代码并没有用户名密码每base64验证登陆的//不用javaMail发送邮件
    import java.net.*;
    import java.io.*;
    import java.util.*;
    //import javax.mail.*;
    //import javax.mail.internet.*;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("[email protected]",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 ("xinxin");



    try{
      Socket s=new Socket(host,25);
      is=new BufferedReader(new InputStreamReader(s.getInputStream()));
      os=new PrintStream(s.getOutputStream());
      }
     catch(NoRouteToHostException e){
      die(/*EX_TEMPFAIL*/1,"No route to host "+host);
      }
     catch(ConnectException e){
      die(/*EX_TEMPFAIL*/1,"Connection Refused by"+host);
      } 
     catch(UnknownHostException e){
      die(/*EX_NOHOST*/2,"Unknown host "+host);
      }
     catch(IOException e){
      die(/*EX_IOERR*/3,"I/O error setting up socket stream\n"+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+"\r\n");
    }

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

    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(/*EX_PROTOCOL*/4,"did not get SMTP greeting");
      
      send_cmd("HELO","[email protected]");
      if(!expect_reply("250")) die(/*EX_PROTOCOL*/4,"did not get ack our HELO");
      send_cmd("RSET");
      if(!expect_reply("250")) die (/*EX_PROTOCOL*/4,"not reset");
      
      send_cmd("AUTH LOGIN");
      if(!expect_reply("334")) die (/*EX_PROTOCOL*/4,"not reset");
      
      send_cmd(enc.encode(user.getBytes()));
      if(!expect_reply("334")) die (/*EX_PROTOCOL*/4,"not reset");
      
     send_cmd(enc.encode(psw.getBytes()));
      if(!expect_reply("334")) die (/*EX_PROTOCOL*/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+"\r");
       
       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");
           
      
      }

    }