// Updates: 2004.08.19


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


/** 
 * This program sends mails. Call it by<br>
 * <code>java Mailer filename</code><br>
 * where filename designs the file containing the data. For exemple, the
 * following file:<br>
 * <pre>
mail.cui.unige.ch
sven@etu.unige.ch, Liam.Bron@cui.unige.ch
Donald.Duck@disney.com
You won a price!
picture.jpg
Hi!

Please visit me at http://disney.go.com/ asap!

See you!

Donald
 * </pre>
 * will use "mail.cui.unige.ch" as mail server to send<br>
 * TO: sven@etu.unige.ch, Liam.Bron@cui.unige.ch<br>
 * FROM: Donald.Duck@disney.com<br>
 * SUBJECT: You won a price!<br>
 * ATTACHMENTS: picture.jpg <i>(if no attachment, write "-")</i><br>
 * BODY: <i>the rest of the file</i><br><br>
 * Note that this program needs mail.jar and activation.jar (javamail).
 * @version 0.1
 * @author Michel Deriaz 
 */
public class Mailer {


  public Mailer(String filename) {
    String text = readFile(filename).replaceAll("\r\n", "\n").replaceAll("\r", "\n");
    int index1 = text.indexOf("\n");
    int index2 = text.indexOf("\n", index1+1);
    int index3 = text.indexOf("\n", index2+1);
    int index4 = text.indexOf("\n", index3+1);
    int index5 = text.indexOf("\n", index4+1);
    String server = text.substring(0, index1);
    String to = text.substring(index1+1, index2);
    String from = text.substring(index2+1, index3);
    String subject = text.substring(index3+1, index4);
    String attachementFile = text.substring(index4+1, index5);
    String body = text.substring(index5+1);
    if (attachementFile.equals("-")) send(server, to, from, subject, body, null);
    else send(server, to, from, subject, body, attachementFile);
  }


  public static void main(String[] args) {
    new Mailer(args[0]);
  }


  private void send(String smtpServer, String to, String from, String subject, String body, String attachementFile) {
    try {
      Properties props = System.getProperties();
      props.put("mail.smtp.host", smtpServer);
      Session session = Session.getDefaultInstance(props, null);
      Message msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress(from));
      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
      msg.setSubject(subject);
      msg.setSentDate(new Date());
      if (attachementFile == null) msg.setText(body);
      else {
        Multipart multipart = new MimeMultipart();
        MimeBodyPart mbp = new MimeBodyPart();
        mbp.setText(body);
        multipart.addBodyPart(mbp);
        mbp = new MimeBodyPart();
        DataSource source = new FileDataSource(attachementFile);
        mbp.setDataHandler(new DataHandler(source));
        mbp.setFileName(attachementFile);
        multipart.addBodyPart(mbp);
        msg.setContent(multipart);
      }
      Transport.send(msg);
      System.out.println("Message sent.");
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }


  private String readFile(String filename) {
    File f = new File(filename);
    int size = (int)f.length();
    char cbuf[] = new char[size];
    try {
      FileReader fr = new FileReader(filename);
      fr.read(cbuf);
      fr.close();
    }
    catch(IOException ioe) {
      return null;
    }
    return  new String(cbuf);
  }
}