/* ** j###t ########## #### #### ** j###t ########## #### #### ** j###T "###L J###" ** ######P' ########## ######### ** ######k, ########## T######T ** ####~###L #### ** #### q###L ########## .##### ** #### \###L ########## #####" ** ** $Id$ ** ** This code was originally written by ** James Driscoll, maus@io.com ** ** I've 'tidied' it up a little. The ** copy I had didn't work. *shrug* ** ** Class History ** ** Date Name Description ** ---------|------------|----------------------------------------------- ** 19Aug98 subtle start of recorded history ** */ package key.util; import java.net.*; import java.io.*; import java.util.*; public class SMTP { public static final int DEFAULT_PORT = 25; protected DataInput reply = null; protected PrintStream send = null; protected Socket sock = null; public SMTP( String hostid ) throws UnknownHostException, IOException { this( hostid, DEFAULT_PORT ); } public SMTP( String hostid, int port ) throws UnknownHostException, IOException { sock = new Socket( hostid, port ); reply = new DataInputStream( sock.getInputStream() ); send = new PrintStream( sock.getOutputStream() ); String recv = reply.readLine(); if( !recv.startsWith( "220" ) ) throw new ProtocolException( recv ); while( recv.indexOf( '-' ) == 3 ) { recv = reply.readLine(); if( !recv.startsWith( "220" ) ) throw new ProtocolException( recv ); } } public SMTP( InetAddress address ) throws IOException { this( address, DEFAULT_PORT ); } public SMTP( InetAddress address, int port ) throws IOException { sock = new Socket( address, port ); reply = new DataInputStream( sock.getInputStream() ); send = new PrintStream( sock.getOutputStream() ); String recv = reply.readLine(); if( !recv.startsWith( "220" ) ) throw new ProtocolException( recv ); while( recv.indexOf( '-' ) == 3 ) { recv = reply.readLine(); if( !recv.startsWith( "220" ) ) throw new ProtocolException( recv ); } } public void send( String from_address, String to_address, String subject, String message ) throws IOException, ProtocolException { String recv; send.println( "HELO smtp" ); recv = reply.readLine(); if( !recv.startsWith( "250" ) ) throw new ProtocolException(recv); send.println( "MAIL FROM: " + from_address ); recv = reply.readLine(); if( !recv.startsWith( "250" ) ) throw new ProtocolException(recv); send.println( "RCPT TO: " + to_address ); recv = reply.readLine(); if( !recv.startsWith( "250" ) ) throw new ProtocolException(recv); send.println("DATA"); recv = reply.readLine(); if( !recv.startsWith( "354" ) ) throw new ProtocolException( recv ); send.println( "From: " + from_address ); send.println( "To: " + to_address ); send.println( "Subject: " + subject ); send.println( "Date: " + msgDateFormat( new Date() ) ); send.println( "Comment: Automated message" ); send.println( "X-Mailer: Key Smtp" ); // Sending a blank line ends the header part. send.println(); // Now send the message proper send.println( message ); send.println( "." ); recv = reply.readLine(); if( !recv.startsWith( "250" ) ) throw new ProtocolException( recv ); } protected void finalize() throws Throwable { send.println( "QUIT" ); sock.close(); super.finalize(); } private static String Day[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; private static String Month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; private final String msgDateFormat( Date senddate ) { StringBuffer formatted = new StringBuffer(); formatted.append( Day[ senddate.getDay() ] ); formatted.append( ", " ); formatted.append( String.valueOf( senddate.getDate() ) ); formatted.append( " " ); formatted.append( Month[ senddate.getMonth() ] ); formatted.append( " " ); if( senddate.getYear() > 99 ) formatted.append( String.valueOf( senddate.getYear() + 1900 ) ); else formatted.append( String.valueOf( senddate.getYear() ) ); formatted.append( " " ); appendLeadingZeroNumber( senddate.getHours(), formatted ); formatted.append( ":" ); appendLeadingZeroNumber( senddate.getMinutes(), formatted ); formatted.append( ":" ); appendLeadingZeroNumber( senddate.getSeconds(), formatted ); formatted.append( " " ); if( senddate.getTimezoneOffset() < 0 ) formatted.append( "+" ); else formatted.append( "-" ); appendLeadingZeroNumber( senddate.getTimezoneOffset()/60, formatted ); appendLeadingZeroNumber( senddate.getTimezoneOffset()%60, formatted ); return( formatted.toString() ); } private final void appendLeadingZeroNumber( int num, StringBuffer sb ) { if( num < 10 ) sb.append( "0" ); sb.append( String.valueOf( num ) ); } }