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
 

"Anyone Out There?"
Vol. 2, Issue 10, p. 52

	

Listing 1.
 
Socket smtpHost; 
BufferedReader In; 
DataOutputStream Out; 

smtpHost = new Socket( “mail2.n-ary.com”, 25 ); 
Out  = new DataOutputStream( smtpHost.getOutputStream() ); 
In  = new BufferedReader( new InputStreamReader( smtpHost.getInputStream() )); 

Listing 2.
 
String LineIn = In.readLine(); 
if ( LineIn.indexOf("220") == -1 ) throw new Exception(“Bad Server”); 

Listing 3.
 
Out.writeBytes( "HELO n-ary.com\r\n" ); 
LineIn = In.readLine(); 
if ( LineIn.indexOf("250") == -1 ) throw new Exception(“Bad return”); 

Listing 4.
 
//- Set the FROM 
Out.writeBytes( "MAIL FROM:<" + _from + ">\r\n" ); 
LineIn = In.readLine(); 
if ( LineIn.indexOf("250")==-1 ) throw new Exception("Bad MAIL FROM:"); 

//- Set the TO field 
Out.writeBytes( "RCPT TO:<" + _to + ">\r\n" ); 
LineIn = In.readLine(); 
if ( LineIn.indexOf("250")==-1 ) throw new Exception("Bad RCPT TO:"); 

Listing 5.
 
//- Set the DATA field 
Out.writeBytes( "DATA\r\n" ); 
LineIn = In.readLine(); 
if ( LineIn.indexOf("354")==-1 ) throw new Exception("Bad DATA"); 
Out.writeBytes( "From: " +_to + "\r\n” ); 
Out.writeBytes( “Subject: " + _subject + "\r\n"); 

Out.writeBytes( _body ); 
Out.writeBytes( "\r\n.\r\n" ); 

Listing 6.
 
//- Set the DATA field 
sendEmail S; 

S = new sendEmail( "mail2.n-ary.com" ); 
S.send( "[email protected]", "[email protected]", "Hello", "Hello Ceri, how are you?" ); 
S.send( "[email protected]", "[email protected]", "Hello", "Hey Frode, hows it going." ); 
S.close(); 

Listing 7.
 
import java.net.*; 
import java.io.*; 

class sendEmail extends java.lang.Object 
{ 
 private Socket smtpHost; 
 private BufferedReader In; 
 private DataOutputStream Out; 

 public sendEmail(String _Host) 
 { 
  try 
  { 
   smtpHost = new Socket( _Host, 25 ); 
   Out = new DataOutputStream( smtpHost.getOutputStream() ); 
   In = new BufferedReader( new InputStreamReader(smtpHost.getInputStream()) ); 

   //- Read Welcome message from server 
   String LineIn = In.readLine(); 
   if ( LineIn.indexOf("220")==-1 ) throw new Exception("Bad Server"); 

   //- Introduce ourselves to the server 
   Out.writeBytes( "HELO n-ary.com\r\n" ); 
   LineIn = In.readLine(); 
   if ( LineIn.indexOf("250")==-1 ) throw new Exception(); 
  } 
  catch( Exception E ) 
  { 
   smtpHost = null; 
  } 
 } 

 public boolean send( String _to, String _from, String _subject, String _body ) 
 { 
  //- Send One Email 
  if ( smtpHost == null ) return false; 

  try{ 
   String LineIn; 

   //- Set the FROM 
   Out.writeBytes( "MAIL FROM:<" + _from + ">\r\n" ); 
   LineIn = In.readLine(); 
   if ( LineIn.indexOf("250")==-1 ) throw new Exception("Bad MAIL FROM:"); 

   //- Set the TO field 
   Out.writeBytes( "RCPT TO:<" + _to + ">\r\n" ); 
   LineIn = In.readLine(); 
   if ( LineIn.indexOf("250")==-1 ) throw new Exception("Bad RCPT TO:"); 

   //- Set the DATA field 
   Out.writeBytes( "DATA\r\n" ); 
   LineIn = In.readLine(); 
   if ( LineIn.indexOf("354")==-1 ) throw new Exception("Bad DATA"); 
   Out.writeBytes( "From: " +_to + "\r\n” ); 
   Out.writeBytes( “Subject: " + _subject + "\r\n"); 

   Out.writeBytes( _body ); 
   Out.writeBytes( "\r\n.\r\n" ); 

   LineIn = In.readLine(); 
   if ( LineIn.indexOf("250")==-1 ) throw new Exception("Bad End of Data"); 

  }catch(Exception E) 
  { 
   System.out.println( "send: " + E ); 
   return false; 
  } 

  return true; 
 } 

 public void close() 
 { 
  try{ 
   if ( smtpHost != null ) smtpHost.close(); 
  }catch( Exception E ){} 
 } 

 protected void finalize() throws Throwable 
 { 
  close(); 
 } 
}

 

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.