     package ORG.anser.it.spd.croft.net.mail;
 
     import java.util.Properties;

     import javax.mail.*;
     import javax.mail.internet.*;

     /*********************************************************************
     * <P>
     * Relies upon the JavaMail API.
     * <P>
     * @author
     *   <A HREF="http://www.alumni.caltech.edu/~croft/">David W. Croft</A>
     * @version
     *   1998-01-25
     *********************************************************************/

     public final class  SMTPMail {
     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     public static final String  TEST_HOST    = "access.mountain.net";
     public static final String  TEST_FROM    = "croft@orbs.com";
     public static final String  TEST_TO      = "croft@computer.org";
     public static final String  TEST_SUBJECT = "SMTPMail test";
     public static final String  TEST_MESSAGE = "This is a test.";
     public static final String  TEST_BASE
       = "http://www.alumni.caltech.edu/~croft/";
     public static final String  TEST_ATTACH
       = "<HTML>\n<HEAD>\n<TITLE>SMTPMail Test</TITLE>\n</HEAD>\n<BODY>\n"
       + "<H1>SMTPMail Test</H1>\n<P>\n"
       + "Content-Base Test:  <A HREF=\"./\">"
       + TEST_BASE
       + "</A>.\n</BODY>\n</HTML>";

     //////////////////////////////////////////////////////////////////////

     /*********************************************************************
     * Command line arguments:
     *   host, from, to, subject, message, attachment (HTML).
     * Will use TEST constants if arguments are not provided.
     *********************************************************************/
     public static final void  main ( String [ ]  args )
       throws AddressException, MessagingException
     //////////////////////////////////////////////////////////////////////
     {
       String  host    = ( args.length < 1 ? TEST_HOST    : args [ 0 ] );
       String  from    = ( args.length < 2 ? TEST_FROM    : args [ 1 ] );
       String  to      = ( args.length < 3 ? TEST_TO      : args [ 2 ] );
       String  subject = ( args.length < 4 ? TEST_SUBJECT : args [ 3 ] );
       String  message = ( args.length < 5 ? TEST_MESSAGE : args [ 4 ] );
       String  attach  = ( args.length < 6 ? TEST_ATTACH  : args [ 5 ] );
       String  base    = ( args.length < 7 ? TEST_BASE    : args [ 6 ] );

       send ( host, from, to, subject, message, attach, base );
     }

     /*********************************************************************
     * Sends a plain text message with no attachments.
     *********************************************************************/
     public static void  send (
       String  mail_server_host,
       String  email_from,
       String  email_to,
       String  subject,
       String  message )
       throws AddressException, MessagingException
     //////////////////////////////////////////////////////////////////////
     {
       send ( mail_server_host, email_from, email_to, subject, message,
         ( String ) null, ( String ) null );
     }

     /*********************************************************************
     * Sends a message with an HTML String as an attachment.
     * <P>
     * <I>html_attachment</I> may be null.
     * <I>content_base</I> may be null.
     *********************************************************************/
     public static void  send (
       String  mail_server_host,
       String  email_from,
       String  email_to,
       String  subject,
       String  text_message,
       String  html_attachment,
       String  content_base )
       throws AddressException, MessagingException
     //////////////////////////////////////////////////////////////////////
     {
       MimeBodyPart [ ]  mimeBodyParts
         = new MimeBodyPart [ html_attachment != null ? 2 : 1 ];
       mimeBodyParts [ 0 ] = new MimeBodyPart ( );
       mimeBodyParts [ 0 ].setText ( text_message );
       if ( html_attachment != null )
       {
         InternetHeaders  internetHeaders = new InternetHeaders ( );
         internetHeaders.addHeader ( "Content-Type", "text/html" );
         if ( content_base != null )
           internetHeaders.addHeader ( "Content-Base", content_base );
         mimeBodyParts [ 1 ] = new MimeBodyPart ( internetHeaders,
           html_attachment.getBytes ( ) );
       }
       send (
         mail_server_host,
         ( Authenticator ) null,
         new InternetAddress [ ] { new InternetAddress ( email_from ) },
         new InternetAddress [ ] { new InternetAddress ( email_to   ) },
         ( InternetAddress [ ] ) null,
         ( InternetAddress [ ] ) null,
         subject,
         mimeBodyParts );
     }
	
     public static void  send (
       String               mail_server_host,
       Authenticator        authenticator,
       InternetAddress [ ]  addresses_from,
       InternetAddress [ ]  addresses_to,
       InternetAddress [ ]  addresses_cc,
       InternetAddress [ ]  addresses_bcc,
       String               subject,
       MimeBodyPart [ ]     mimeBodyParts )
       throws MessagingException
     //////////////////////////////////////////////////////////////////////
     {
       Properties  properties = new Properties ( );
       properties.put ( "mail.smtp.host", mail_server_host );
	
       MimeMessage  msg = new MimeMessage (
         Session.getInstance ( properties, authenticator ) );

       msg.addFrom ( addresses_from );
       msg.setRecipients ( Message.RecipientType.TO , addresses_to  );
       msg.setRecipients ( Message.RecipientType.CC , addresses_cc  );
       msg.setRecipients ( Message.RecipientType.BCC, addresses_bcc );
       msg.setSubject ( subject );

       MimeMultipart  mimeMultipart = new MimeMultipart ( );
       for ( int  i = 0; i < mimeBodyParts.length; i++ )
       {
         mimeMultipart.addBodyPart ( mimeBodyParts [ i ] );
       }
       msg.setContent ( mimeMultipart );

       Transport.send ( msg );
     }

     private  SMTPMail ( ) { }
     //////////////////////////////////////////////////////////////////////

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     }
