一、接受电子邮件的主程序POP3Frame.java
//POP3Frame.java
    import java.io.*;
    import java.net.*;
    public class POP3Frame extends JFrame  {    
       //初始化菜单条、标签、单行文本框、文本区、面板
       //初始化按钮
     JButton jButtonreceive=new JButton("查看");
      
     public  POP3Frame(){
           .......
      createPOP3Frame();
      
      }          
        private  void createPOP3Frame() throws Exception{       
           ....         
          //给按钮加上事件
              jButtonreceive.addActionListener(new 
              java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e){
                jButtonreceive_actionPerformed(e);
               }
             });
          .....
          }  
                      
        public  void start(){}         
        public  void stop(){ }         
        public  void  destroy(){ }         
                
       void jButtonreceive_actionPerformed(ActionEvent e){
          Thread pop3thread=new Thread((new ReceiveMail(this)));
          pop3thread.start();
           }
           
 }  //end POP3Frame.java
       
这个主程序运行没有问题

解决方案 »

  1.   

    二、接受邮件的具体过程ReceiveMail.java import java.io.*;
     import java.net.*;
         public class ReceiveMail extends Thread  {
         private BufferedReader inData;
         private BufferedWriter outData;
         private Socket cs;     
         private  static String host,port; 
          ....
          public void run(){   
             loadProperties();          
               if (receiv()){
                    .....
                boolean  flag=showmail(currentMail);
                showObject(true);
              }
          else{   showObject(false);}
               }//end public void run() 
          
           void loadProperties(){
               Properties p=new Properties();          //POP3.properties中保存 主机名、端口
               FileInputStream in=new FileInputStream("POP3.properties");        p.load(in);
               host=p.getProperty("pop3.host");//主机名
               port=p.getProperty("pop3.port");//端口
           .....
            in.close();
            }  //end    void loadProperties()
     
          boolean receiv() throws IOException{
           ....       
          try{  // try1          
            try{
             cs=new Socket(InetAddress.getByName(host),Integer.parseInt(port));
               }
             catch  (ConnectException  cserror){
              cserror.printStackTrace();
              }        
            inData=new BufferedReader(new InputStreamReader(cs.getInputStream()));
            outData=new BufferedWriter(new OutputStreamWriter(cs.getOutputStream()));
            ....
            catch (Exception ex){
       ex.printStackTrace();
       try{ 
                 outData.close();
           inData.close();
           cs.close();}
                 catch(IOException ioex){}
                 return  false;}//end  catch (Exception ex)
             }//end  try1
          catch(UnknownHostException e1) {
      e1.printStackTrace();
      return false;}      catch(IOException ex) {
      ex.printStackTrace();
      return false;}
      }//end boolean receiv() throws IOException
          
         boolean  showmail(int mailNo){.....}
         void  showObject(boolean  flag) {....}
    }//end ReceiveMail.java编译它没有问题。主要有两种情况:
    第一、如果在POP3服务器中设置:
      主机名:pop3.mail.sohu.com 在主程序中按下“查看”按钮jButtonreceive,出现如下异常:
    java.net.ConnectException: Connection timed out: connect
    java.lang.NullPointerException出错位置:
      cs=new Socket(InetAddress.getByName(host),Integer.parseInt(port));
               }
            catch  (ConnectException  cserror){
              cserror.printStackTrace();
            }        
         inData=new BufferedReader(new InputStreamReader(cs.getInputStream()));
         outData=new BufferedWriter(new OutputStreamWriter(cs.getOutputStream()));
    第二、如果在POP3服务器中设置:
      主机名:pop.mail.sohu.com 
      出现java.net.UnknownHostException: pop.mail.sohu.com: pop.mail.sohu.com这个异常
      出错位置:
      cs=new Socket(InetAddress.getByName(host),Integer.parseInt(port));
      

  2.   

    三、发送电子邮件的主程序SMTPFrame.java
         import java.io.*;
        import java.net.*;
        public class SMTPFrame extends JFrame  {    
           //初始化菜单条、标签、单行文本框、文本区、面板
           //初始化按钮
         JButton jButtonsend=new JButton("发送");
          
         public  SMTPFrame(){
               .......
          createSMTPFrameFrame();
          
          }          
            private  void createSMTPFrame() throws Exception{       
               ....         
              //给按钮加上事件
                  jButtonreceive.addActionListener(new 
                  java.awt.event.ActionListener() {
                    public void actionPerformed(ActionEvent e){
                    jButtonsend_actionPerformed(e);
                   }
                 });
              .....
              }  
                          
            public  void start(){}         
            public  void stop(){ }         
            public  void  destroy(){ }         
                    
           void jButtonsend_actionPerformed(ActionEvent e){
              Thread thread=new Thread((new sendMail(this)));
              thread.start();
               }
               
     }  //end SMTPFrame.java 这个程序正常
      

  3.   

    四、发送邮件的过程
    //sendMail.java
               public class sendMail extends Thread implements Runnable {
         
          private BufferedReader inData;
          private BufferedWriter outData;
          private Socket cs;
           ........     
          private SMTPFrame  frame;
              
          public sendMail(SMTPFrame  frame){
          this.frame=frame;
                 ....... }            
            public void run(){
          ....
            try{//try2
            cs=new Socket(InetAddress.getByName(host),Integer.parseInt(port));
              try{        
            inData=new BufferedReader(new InputStreamReader(cs.getInputStream()));
            outData=new BufferedWriter(new OutputStreamWriter(cs.getOutputStream()));
           .... }
           
      catch (Exception ex){
       ex.printStackTrace();
       try{
       outData.close();
                      in.cloase();
       cs.close();} 
            catch (IOException ioex) {
       ioex.printStackTrace();}
           }
     } //try2   catch(UnknownHostException e1) {
      e1.printStackTrace();}    catch(IOException ex) {
      ex.printStackTrace();} }
    }//end sendMail.java
    这个程序没有问题。麻烦大家帮我看看运行接受邮件的具体过程ReceiveMail.java出错是怎么回事?
    不胜感激!!
      

  4.   

    用telnet连接看看是否正常
      

  5.   

    POP3 服务器:pop3.sohu.com 
    POP3 端  口:110 俺特意去注册了一个账号,应该不会错了吧
      

  6.   

    遇到这种错误,还是比较容易判断的。
    如果是UnknownHostException,那么一定是地址有问题。看了楼上几位的回答,总的来说估计是地址错误问题。
    我试了一下, telnet pop3.sohu.com 110
    能够连接到该地址,其他都不可以。
    那么是明显的地址错误。楼上的正确。
      

  7.   

    其实像这种错误是可以避免的,写邮件程序一般总要去你邮箱的ISP去看一下设置啦。
      

  8.   

    先谢谢大家。
     一、 确实是在POP3服务器中设置:
      主机名应该设置为pop3.sohu.com 。
      这一步是应该这样设置。
    二、我好好的运行了发送邮件程序SMTPFrame.java    比如我输入的源地址是[email protected],发送端口是25
        Subject(邮件标题)是:myemail
        Content(邮件内容)是:test the email    但是在发送电子邮件的主程序SMTPFrame中按下“发送”按钮jButtonsend显示如下邮件处理过程:
        c:Trying to connect to hostsmtp.mail.yahoo.com.cn,port:25c:connect to hostsmtp.mail.yahoo.com.cn,port:25S:220smtp.mail.yahoo.com.cnsimple mail transfer service readyC:HELOsmtp.mail.yahoo.com.cnS:250502 unimplemented (#5.5.1)S:250smtp.mail.yahoo.com.cnC:MAIL FROM:[email protected]:RCPT TO:<[email protected]>S:250530 authentication required - for help go to http://help.yahoo.com/help/us/mail/pop/pop-11.htmlS:250 okC:DATAC:Date:星期二,12 三月 2013 08:56:07 CSTC:From:[email protected]:To:[email protected]:Subject:myeamilC:C:.C:QUIT
    问题在于看不到邮件的内容。   下面是发送邮件过程的程序sendMail.java的部分内容
      

  9.   

    //下面发送邮件的过程:
     public class sendMail extends Thread implements Runnable {
      ......  public void run(){
       String request;
        String  msg;
        
        JTextArea jLog=new  JTextArea();            
         try{
            
        frame.jLog.append("c:Trying to connect to   host"+host+",port:"+port+linesep);
       cs=new Socket(InetAddress.getByName(host),Integer.parseInt(port));             
    frame.jLog.append("c:connect to host"+host+",port:"+port+linesep);
          try{
         inData=new BufferedReader(new InputStreamReader(cs.getInputStream()));
         outData=new BufferedWriter(new OutputStreamWriter(cs.getOutputStream()));
            
         msg="220"+host+"simple mail transfer service ready";
         outData.write('\r');
         outData.write('\n');
         //和服务器建立连接   
         msg="HELO"+host;
         outData.write('\r');
         outData.write('\n');
         //服务器返回连接建立信息     
         msg="250"+host;
         outData.write('\r');
         outData.write('\n');
         //发出邮件地址    
         msg="MAIL FROM:<"+addr.trim()+">";
         outData.write('\r');
         outData.write('\n');
            
        //发出目的地址信息     
         msg="RCPT TO:<"+rcpto+">";
         outData.write('\r');
         outData.write('\n');       
           }
       //开始发邮件内容
        msg="DATA";
        outData.write('\r');
        outData.write('\n'); 
           
         msg="354 start mail input;end with <CRLF>.<CRLF>";  
    //源地址             
        msg="From:"+addr.trim();
       outData.write('\r');
       outData.write('\n');
     //目的地址      
       msg="To:"+to;
       outData.write('\r');
       outData.write('\n');
      //邮件标题   
       msg="Subject:"+subject;
       outData.write('\r');
       outData.write('\n');
            
    //就是在这里输不出邮件内容        
       StringBuffer content=new StringBuffer(msgText.length());        
       int k=0,l;
       while ((l=msgText.indexOf("\n.",k))!=-1){
          content.append(msgText.substring(k,l)+"\n..");
             k=l+2;
        }
        if (k>0) {
         content.append(msgText.substring(k));
         msg=content.toString();
        }else{
         msg=msgText;
        }
        outData.write('\r');
        outData.write('\n');
       //邮件结尾 
       msg=String.valueOf(".");
       outData.write('\r');
       outData.write('\n');  
       //退出    
        msg="QUIT";
        outData.write('\r');
        outData.write('\n');  
         }
      catch (Exception ex){
       ex.printStackTrace();
       try{
       outData.close();
       inData.close();
       cs.close();
      
      }
      catch (IOException ioex) {
       ioex.printStackTrace();
      }
    }
    }
    //就是在下面这一段输不出邮件内容 ,是怎么回事?       
       StringBuffer content=new StringBuffer(msgText.length());        
       int k=0,l;
       while ((l=msgText.indexOf("\n.",k))!=-1){
          content.append(msgText.substring(k,l)+"\n..");
             k=l+2;
        }
        if (k>0) {
         content.append(msgText.substring(k));
         msg=content.toString();
        }else{
         msg=msgText;
        }
        outData.write('\r');
        outData.write('\n');
       //邮件结尾 
       msg=String.valueOf(".");
       outData.write('\r');
       outData.write('\n');
      

  10.   

    三、接受邮件程序ReceiveMail.java   在POP3服务器中设置:
      主机名:pop.mail.sohu.com 
           pop port: 110
      邮件地址:[email protected]
     boolean receiv() throws IOException{
          
        String  msg;
           
        try{ //1
         frame.jLog.append("c:Trying to connect to host"+host+",port:"+port+linesep);
         cs=new Socket(InetAddress.getByName(host),Integer.parseInt(port));
         frame.jLog.append("c:connect to host"+host+",port:"+port+linesep);
          try{//2        
           inData=new BufferedReader(new InputStreamReader(cs.getInputStream()));        
         outData=new BufferedWriter(new OutputStreamWriter(cs.getOutputStream()));
            
        //检查帐号
             msg="USER"+name; 
            outData.write('\r');
            outData.write('\n');
         //检查密码           
            msg="PASS"+pass;   
             outData.write('\r');
            outData.write('\n');
                 
             msg="STAT";   
            outData.write('\r');
            outData.write('\n');
                     
           StringTokenizer tokenizer=new  StringTokenizer(response," ");
              
           int k=0;
            
            while (tokenizer.hasMoreTokens()){
             k++;
             String temp=tokenizer.nextToken().trim();
            
             if(k==2){
             totalMail=Integer.parseInt(temp);
                  }
            if(k==3){
             mailSize=Integer.parseInt(temp);
                   }
             }//endd while
            return true;
        }//end  2              
      catch (Exception ex){
       ex.printStackTrace();
       try{
       outData.close();
       inData.close();
       cs.close();
           }
      catch (IOException ioex) {}
      // ioex.printStackTrace();
       return false;
       }
    }
     catch(UnknownHostException e1) {
      e1.printStackTrace();
      return false;
    }
    catch(IOException ex) {
      ex.printStackTrace();
      return false;
    }}运行这个程序显示邮件处理过程。但是却显示[email protected]这个帐号不存在
    c:Trying to connect to hostpop3.sohu.com,port:110c:connect to hostpop3.sohu.com,port:110S:+OK pop.mail.sohu.com POP3 Server ReadyC:USER456S:-ERR Error: command not implemented
      

  11.   

    现在好多smtp需要身份验证,是不是这个问题
      

  12.   

    java.net.ConnectException: Connection timed out: connect
    ==> fire wall problem? or Server is down? or Server address is incorrect?
    solution: ping the server or telnet to the server
    What's the stack trace of "java.lang.NullPointerException"?
      

  13.   

    try use javamail:
    http://expert.csdn.net/Expert/topic/1197/1197493.xml?temp=.4232752
      

  14.   

    好像sohu的邮箱很慢
    你把POP3置为pop3.sohu.com.cn试试。
      

  15.   

    这几天sohu的邮件服务器好像有点问题。尤其是pop3。你换别的服务器试试。
      

  16.   

    telnet pop3.sohu.com 110
    但是等我输入完毕密码后,就被服务器关闭连接了。
      

  17.   

    我在运行框输入
    telnet pop3.sohu.com 110
    显示:
    +OK pop.mail.sohu.com POP3 Server Ready 
    等一会显示到主机的连接丢失