Java Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class MailApplet extends JApplet implements ActionListener
{
JTextArea msg;
JLabel messageLabel, status;
JButton submit;
public void init( )
{
messageLabel = new JLabel( "Enter your message: " );
status = new JLabel( );
msg = new JTextArea(5,20);
submit = new JButton( "Submit" );
submit.addActionListener( this );
setLayout( new FlowLayout( ) );
add( messageLabel ); add( msg ); add( submit ); add( status );
}
public void actionPerformed( ActionEvent ae )
{
Object src = ae.getSource( );
if ( src == submit )
{
String message = msg.getText( );
String s = sendMail( "http://www.cs.colostate.edu/~boese/cgi-bin/mailer.cgi?message=",message );
status.setText(s);
}
}
public String sendMail( String fullPath, String msg )
{
try
{
String enc = URLEncoder.encode(msg, "UTF-8");
URL target = new URL( fullPath + enc );
String content;
URLConnection con = target.openConnection( ); // open connection
con.setUseCaches( false );
con.setDefaultUseCaches( false );
byte b[ ] = new byte[ 1024 ]; // byte array
int nbytes; // num bytes read in
String retVal = new String();
BufferedInputStream in = new BufferedInputStream( con.getInputStream(), 2048 );
while( (nbytes = in.read( b, 0, 1024 )) != -1 ) // while there's more data to read
{
content = new String( b, 0, nbytes ); // get 1024 bytes of data fr file
retVal += content;
}
in.close( ); // close connection
return retVal;
}
catch( Exception e )
{
return "Error " + e.toString( );
}
}
}
©2007-
by E.S.Boese. All Rights Reserved