HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"POP Goes the Server"
Vol. 2, Issue 12, p. 64

	

Listing 1. 

BufferedReader      In; 
DataOutputStream    Out; 
Socket              OutPort; 
OutPort = new Socket( "mail.n-ary.com", 110 ); 
Out     = new DataOutputStream( OutPort.getOutputStream() ); 
In      = new BufferedReader( new InputStreamReader(OutPort.getInputStream()) 
); 

Listing 2. 

sendMessage( "USER " + userName ); 
if ( rxdLine("+OK") == null ) throw new Exception( "Invalid username" ); 

sendMessage( "PASS " + passWord ); 
if ( rxdLine("+OK") == null ) throw new Exception( "Invalid password" ); 

Listing 3. 

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

public class popMail extends Object 
{ 
    BufferedReader      In; 
    DataOutputStream    Out; 
    Socket              OutPort; 
    String host; 
    String userName; 
    String passWord; 

    public popMail(String _Host, String _userName, String _passWord) 
    { 
        host        = _Host; 
        userName    = _userName; 
        passWord    = _passWord; 
    } 

    public boolean open() throws Exception 
    { 
        return open( 110 ); 
    } 

    public boolean open( int _port ) throws Exception 
    { 
        try{ 
            OutPort = new Socket( host, _port ); 
            Out     = new DataOutputStream( OutPort.getOutputStream() ); 
       In      = new BufferedReader( new 
InputStreamReader(OutPort.getInputStream()) ); 

            if ( rxdLine("+OK") == null ) throw new Exception( "Server did not 
say hello." ); 

            //- Send Username 
            sendMessage( "USER " + userName ); 
            if ( rxdLine("+OK") == null ) throw new Exception( "Invalid 
username" ); 

            sendMessage( "PASS " + passWord ); 
            if ( rxdLine("+OK") == null ) throw new Exception( "Invalid 
password" ); 

            return true; 
        }catch( IOException E ){} 
        throw new Exception( "Server not Ready" ); 
    } 

    public void close() 
    { 
        sendMessage( "QUIT" ); 
        try{ 
            OutPort.close(); 
        }catch( IOException E){} 
    } 

    private void sendMessage( String _M ) 
    { 
        try{ 
           Out.writeBytes( _M + "\r\n" ); 
        }catch(IOException E){} 
    } 

    private String rxdLine( String _command ) 
    { 
        try{ 
            String i = In.readLine(); 
            if ( i.indexOf(_command)==0 ) 
                return i; 
        }catch(IOException E){} 
        return null; 
    } 

    private String rxdLine() 
    { 
        try{ 
            return In.readLine(); 
        }catch(IOException E){} 
        return null; 
    } 

    public int mailNo() 
    { 
        String LineIn; 

        sendMessage( "STAT" ); 
        if ((LineIn=rxdLine("+OK"))==null ) return 0; 

        int x = LineIn.indexOf(" "); 
        LineIn = LineIn.substring(x+1,LineIn.indexOf(" ",x+1)); 

        try{ 
            return Integer.parseInt( LineIn ); 
        }catch (Exception E){} 

        return 0; 
    } 

    public int[] getMailIDs() 
    { 
        String LineIn; 

        sendMessage( "LIST" ); 
        if ((LineIn=rxdLine("+OK"))==null ) return null; 

        int x = LineIn.indexOf(" "); 
        LineIn = LineIn.substring(x+1,LineIn.indexOf(" ",x+1)); 

        int No=0; 
        try{ 
            No = Integer.parseInt( LineIn ); 
        }catch(Exception E){ 
            No = 0; 
        } 

        if (No == 0 ) 
            return null; 

        int list[] = new int[No]; 
        int a=0; 
        while ( (LineIn=rxdLine())!=null ) 
        { 
            if ( LineIn.indexOf(".")!=-1) break; 
            list[a++] = Integer.parseInt( LineIn.substring(0,LineIn.indexOf(" 
"))); 
        } 
        return list; 
    } 

    public wMail getMailHdr( int _id ) 
    { 
        wMail M = new wMail(); 
        M.ID    = _id; 
        String LineIn; 

        sendMessage( "TOP " + _id + " 1" ); 

        while ( (LineIn=rxdLine())!=null ) 
        { 
            if ( LineIn.indexOf(".")==0) 
                break; 
            else if ( LineIn.indexOf("Date:") != -1 ) 
                M.Date = new Date(LineIn.substring(LineIn.indexOf(":")+2, 
LineIn.length())).getTime(); 
            else if ( LineIn.indexOf("Subject:") != -1 ) 
                M.Subject = LineIn.substring( LineIn.indexOf(":")+2, 
LineIn.length() ); 
            else if ( LineIn.indexOf("From:") != -1 ) 
                M.From = LineIn.substring( LineIn.indexOf(":")+2, 
LineIn.length() ); 
            else if ( LineIn.indexOf("+OK") != -1 ) 
            { 
                try{ 
                    int q = LineIn.indexOf("(")+1; 
                    M.Size = Integer.parseInt( LineIn.substring( 
q,LineIn.indexOf(" ",q+1) ) ); 
                }catch(Exception E){} 
            } 
        } 
        return M; 
    } 

    public wMail getMail( int _id ) 
    { 
        wMail M = new wMail(); 
        M.ID    = _id; 
        String LineIn; 

        sendMessage( "RETR " + _id ); 
        boolean bHdr = true; 

        while ( (LineIn=rxdLine())!=null ) 
        { 
            if ( LineIn.indexOf(".")==0 && LineIn.length()==1) 
                break; 
            else if ( LineIn.indexOf("Date:") != -1 ) 
                M.Date = new Date(LineIn.substring(LineIn.indexOf(":") 
                  +2,LineIn.length())).getTime(); 
            else if ( LineIn.indexOf("Subject:") != -1 ) 
                M.Subject = LineIn.substring( LineIn.indexOf(":")+2, 
LineIn.length() ); 
            else if ( LineIn.indexOf("From:") != -1 ) 
                M.From = LineIn.substring( LineIn.indexOf(":")+2, 
LineIn.length() ); 
            else if ( LineIn.indexOf("+OK") != -1 ) 
            { 
                try{ 
                    int q = LineIn.indexOf("(")+1; 
                    M.Size = Integer.parseInt( LineIn.substring( 
q,LineIn.indexOf(" ",q+1) ) ); 
                }catch(Exception E){} 
            } 
            else if ( LineIn.length() == 0 && bHdr) 
            { 
                bHdr = false; 
                M.vBody = new Vector(10,5); 
            } 
            else if ( !bHdr ) 
            { 
                if ( LineIn.indexOf("..") == 0 ) 
                    LineIn = LineIn.substring( 1, LineIn.length() ); 

                M.vBody.addElement( LineIn ); 
            } 
        } 

        return M; 
    } 

    public void deleteMail( int _id ) 
    { 
        sendMessage( "DELE " + _id ); 
        rxdLine(""); 
    } 
} 

class wMail extends Object 
{ 
    public int    ID=0; 
    public int    Size=0; 
    public String Subject=""; 
    public String From=""; 
    public long   Date=0; 
    public Vector vBody=null; 
}
 

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.