/*
 * MailOut: a simple program to email a message with an optional file
 *          attachment using the JavaMail API.  Minimal error checking
 *          is done to reduce code size.  To run this code, you will
 *          need mail.jar and activation.jar from JavaMail and Java
 *          Activation Framework (JAF) respectively.  
 *
 * Author : Chris Ryan (cryan@plugged.net.au)
 *          Plugged In Software
 *
 * Copyright (c) 1999 Plugged In Software
 * Released under GNU GPL - see http://www.gnu.org/copyleft/gpl.html
 *
 * Usage  : MailOut <from> <to> <subject> <message_text> [<filename>]
 *          e.g. java -Dmail.host=mailhost.somewhere.net MailOut me@somewhere.net you@elsewhere.com "Funny Jokes" "See the attached file for some funny jokes." jokes.txt
 * 
 */

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
import java.util.*;

public class MailOut {

  public final static void main(String[] args) throws Exception {

    // Check that the right number of parameters have been passed
    if (!(args.length == 4 || args.length == 5)) {

      System.out.println("Usage: MailOut <from> <to> <subject> <message_text> [<filename>]");
      System.exit(1);

    }

    // Get the parameters from the command line.
    String from = args[0];
    String to = args[1];
    String subject = args[2];
    String message_text = args[3];
    String filename = null;

    // set the filename if we've got one.
    if (args.length == 5) {

      filename = args[4];

    }

    /*
     * Get the system properties.  A number of properties can be
     * set that are useful to the JavaMail system, including
     * mail.host.  If mail.host is not set, JavaMail will use
     * localhost for the default mail server.
     */
    Properties system_properties = System.getProperties();

    // get the default JavaMail Session instance
    Session session = Session.getDefaultInstance(system_properties,null); 

    // switch on the session debugging to see what's happening
    session.setDebug(true);

    try {

      // create a new MIME message
      Message message = new MimeMessage(session);

      /*
       * Create Internet style addresses for 'from' & 'to'.  The
       * assumption is that the default transport being used in
       * this session uses RFC822 addresses. 
       */
      InternetAddress from_internet_address = new InternetAddress(from);

      // assume there might be more than one recipient
      InternetAddress[] recipients = InternetAddress.parse(to);

      // set the 'from' in the message
      message.setFrom(from_internet_address);

      // set the recipient for the message - assume there might be more than one
      message.setRecipients(Message.RecipientType.TO,recipients);

      // set the subject for the message
      message.setSubject(subject);

      // do we have a file to send?
      if (filename != null) {

        // yes - create the individual parts of the MIME message

        // first, create the plain text message body
        MimeBodyPart message_body = new MimeBodyPart();
        message_body.setContent(message_text,"text/plain");

        /*
         * next, create the attachment body part.
         *
         * This requires using the Java Activation Framework to create
         * a DataHandler for the file.  DataHandler provides a consistent interface 
         * to data available in many different sources and formats.
         * In this case, we're using a local filename, so our DataSource for
         * the DataHandler is FileDataSource.  A DataSource provides a type
         * for the data (file) and access to it - I/O streams.  Refer to the
         * JAF API for more details.
         */
        DataSource file_data_source = new FileDataSource(filename);
        DataHandler file_data_handler = new DataHandler(file_data_source);
        MimeBodyPart file_attachment = new MimeBodyPart();
        file_attachment.setDataHandler(file_data_handler);

        // create the Multipart message
        Multipart multipart_message = new MimeMultipart();

        // add the two bodies to this multipart
        multipart_message.addBodyPart(message_body);
        multipart_message.addBodyPart(file_attachment);

        // set the content type of this message to indicate multipart
        message.setContent(multipart_message);

      } else {

        // no - send a plain message
        message.setContent(message_text,"text/plain");

      }

      // send the message using the default transport for this session
      Transport.send(message);

    } catch (MessagingException me) {

      System.err.println("Error sending message: "+me.getMessage());
      System.exit(1);

    }

  }


}
