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.
- 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.
- Use the javax.mail API to send messages.
- Download and unzip the javax.mail implementation from http://java.sun.com/products/javamail/downloads/index.html
- Include javamail-1.4.4/mail.jar in your classpath
- 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.
- From the command line, use -cp option. Example: javac -d bin -cp ~/Desktop/javamail-1.4.4/mail.jar src/*.java
- 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
- Properties props = new Properties();
- props.put("mail.smtp.host", "my-mail-server");
- props.put("mail.from", "me@example.com");
- Session session = Session.getInstance(props, null);
- try {
- MimeMessage msg = new MimeMessage(session);
- msg.setFrom();
- msg.setRecipients(Message.RecipientType.TO,
- "you@example.com");
- msg.setSubject("JavaMail hello world example");
- msg.setSentDate(new Date());
- msg.setText("Hello, world!\n");
- Transport.send(msg);
- } catch (MessagingException mex) {
- System.out.println("send failed, exception: " + mex);
- }
- You may use smtp.usfca.edu as your SMTP server. NOTE, however, that this may not work from off campus.
- The message may contain any characters, including the $. If the $ is to appear literally, it must be escaped as \$.
- At minimum, your program must handle the following erroneous situations: