import java.io.*;
import java.net.*;
import java.util.*;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
 /**
  * This class is designed to test the JavaMail api. It also goes some of the way 
  * to creating a useful command-line or console email sending program
  * @author mjb
  * @see "http://www.ella-associates.org/utils/checkhotmail.java"
  */
public class TestJavaMail 
{
  public static void main (String[]args) throws Exception
  {
   // 1 august 2003. This seems to be working. That is, sending emails
   // I should watch out for hotmail line ending problems
   //
   // This is also working with authentication now
   //
   //
   //
    String sMailServerHost = "mx3.hotmail.com";
    String sFilePath = "addresses.txt";
    String sRecipientAddress = "";
    String sMessageBody = "";
    String sMessageSubject = "";

    if (args.length == 2)
    {
      sRecipientAddress = args[0];
      sMessageBody = args[1];
      sMessageSubject = "none [generated by JavaMail]";
    }
    else if (args.length == 3)
    {
      sRecipientAddress = args[0];
      sMessageBody = args[1];
      sMessageSubject = args[2];
    }
    else 
    {
      System.out.
      println ("TestJavaMail:                                  \n" +
       "-------------------------------                 \n" +
       "  Command line usage:                           \n" +
       "  java TestJavaMail recipient-email-address message [subject] \n\n" +
       "  The purpose of this program is to test the JavaMail api\n" +
       "  list of hotmail accounts. The list is contained in a text file \n" +
       "  where each line of the text file contains one hotmail email \n" +
       "  address.               \n" +
       "                         \n" +
       "  Examples:              \n" +
       "  ---------              \n" +
       "    java TestJavaMail nick@findesign.org 'no more coffee today, thanks' \n" +
       "      The program should send the message specified to N Reddel \n" +
       "                            \n");

       System.exit (-1);

       
    }  //-- if, else [Command Line Arguments]
  
    String host = "ella-associates.org";
    String from = "matthew@ella-associates.org";

    
    // Setup mail server
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.auth", "true");
		 
    javax.mail.Authenticator auth = new SimpleSmtpAuthenticator();
    Session session = Session.getDefaultInstance(props, auth);
    // session.setDebugOut(null);
    
    // Define message and put content in the email message
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(sRecipientAddress));
    message.setSubject(sMessageSubject);
    message.setText(sMessageBody);
    
    System.out.println("About to send message");

    // Send message
    Transport.send(message);
    //   
    System.out.println("Just attempted to send message");
  }   // main
}   // class: TestJavaMail 

// I could make this an anonymous inner class I suppose, to allow parametrization of 
// the username and password
class SimpleSmtpAuthenticator extends javax.mail.Authenticator
{
  public javax.mail.PasswordAuthentication getPasswordAuthentication()
  {
    return new javax.mail.PasswordAuthentication("matthew", "bacibaci");
  }
} 
