Project 2 - An Email Program

Due Friday 10/7/2011 - 11:59pm

The goals of this project are as follows:

    • practice algorithmic thinking
    • practice text processing
    • practice file input and output
    • become familiar with using third-party libraries and setting the classpath
    • become familiar with providing command line input
    • become familiar with the data structures provided in java.util

For this project, you will build a program that sends a customized email message to a list of recipients. The message to be sent will be stored in a text file, and may contain variables denoted by the dollar sign ($). The values for the variables, along with the email address of each recipient, will be stored in a separate file.

Example message file:

This is a test message for $name. You own me \$$amount!

Example values/addresses file:

address = bob@mail.com

name = Bob Smith

amount = 90

address = sally@mail.edu

name = Sally Sue

amount = 20

In the example above, the message "This is a test message for Bob Smith. You owe me $90!" will be sent to bob@mail.com and the message "This is a test message for Sally Sue. You owe me $20!" will be sent to sally@mail.edu.

A message may contain multiple variables. A message may also contain the dollar sign. The escape sequence \$ is used to denote that the dollar sign should appear literally.

Here is an example message file.

Here is an example address file.

Requirements and Hints

    1. The program must accept the name of the message file, the name of the address file, and the 'from' email address as command line input. From the command line, your program should be run as follows: java Driver -M messagefile.txt -A addressfile.txt -F name@cs.usfca.edu From Eclipse, you can configure the command line parameters by choosing Run -> Open Run Dialog -> Arguments. In the Program Arguments box, type -M messagefile.txt -A addressfile.txt -F name@cs.usfca.edu and then run.
    2. Use the javax.mail API to send messages.
      1. Download and unzip the javax.mail implementation from http://java.sun.com/products/javamail/downloads/index.html
      2. Include javamail-1.4.4/mail.jar in your classpath
        1. In Eclipse, right click the mailer project and select Properties. Select Java Build Path on the left. Select Libraries. Select Add External Jars. Navigate to the jar and click Open. Do this for each jar.
        2. From the command line, use -cp option. Example: javac -d bin -cp ~/Desktop/javamail-1.4.4/mail.jar src/*.java
    3. The javadoc for the javax.mail package contains a good example of how to send mail using java: http://java.sun.com/products/javamail/javadocs/index.html?javax/mail/package-summary.html
                1. Properties props = new Properties();
                2. props.put("mail.smtp.host", "my-mail-server");
                3. props.put("mail.from", "me@example.com");
                4. Session session = Session.getInstance(props, null);
                5. try {
                6. MimeMessage msg = new MimeMessage(session);
                7. msg.setFrom();
                8. msg.setRecipients(Message.RecipientType.TO,
                9. "you@example.com");
                10. msg.setSubject("JavaMail hello world example");
                11. msg.setSentDate(new Date());
                12. msg.setText("Hello, world!\n");
                13. Transport.send(msg);
                14. } catch (MessagingException mex) {
                15. System.out.println("send failed, exception: " + mex);
                16. }
      1. You may use smtp.usfca.edu as your SMTP server. NOTE, however, that this may not work from off campus.
    4. The message may contain any characters, including the $. If the $ is to appear literally, it must be escaped as \$.
    5. At minimum, your program must handle the following erroneous situations:

Recommended Design and Implementation

It is recommended that you implement your project in stages as described below:

    • Stage 1 - The program works as described above with the exception that variables and escape sequences in the message file are not processed. A simple text file is read in and sent to a list of email addresses specified in the address file. (worth 70% credit)
    • Stage 2 - The program works as described above with the exception that escape sequences are ignored. A text file is read in and processed to extract the variables. The customized message, including the variable values, is sent to each recipient. (worth 90% credit)
    • Stage 3 - The program works as described above, including escape sequences. (worth 100% credit).
    • Stage 4 - Use SSL (Secure Socket Layer) for communication. See http://java.sun.com/products/javamail/FAQ.html to get started. (extra credit)

There is no API that you are required to implement for this project. The recommended design is as follows:

    • Msg - A class that processes the file containing the message and maintains data structures containing the message and the variable names. One option is to maintain two ArrayLists. One contains an item for each portion of the message and one contains the variable names. For the message "this is message $num for $name", the message list would contain the items [this is a message ] [for] and the variables list would contain [num] [name]. The ith variable value will go after the ith message item.
      • Note: Do not use Message as a class name. You will end up with a conflict with the javax.mail.Message class.
    • Values - A class that processes the file containing the addresses and variable values and maintains data structures containing the information. One option is to maintain an ArrayList of HashMaps. Each item in the ArrayList is a HashMap that maintains the record for a particular recipient. THe HashMap contains key, value pairs where the key is the variable name and the value is the value.
    • Mailer - A class that contains the logic of sending appropriate messages. This class will need to have access to the data structures maintained by Msg and Values.

Submission Instructions

Please submit your work in an SVN directory https://www.cs.usfca.edu/svn/<username>/cs112/project2.