CS 117, Introduction to Computer Science

Sending Email!

Assigned on Wednesday, 1/16/02.
Program due by 9 PM on Monday, 10/21/02.

Overview

For this program, you will create an instantiable class that can store and send an email message!

Specifics

The Message class

Create a new BlueJ project called SendEmail. In this project, create an instantiable class called Message. Message should have a constructor that records the date and time at which an object is created. Additionally, it should have the following public instance methods:

Method Name Parameter(s) Return value Purpose
setFrom String none Record who the message is from
setTo String none Record who the message is to
setSubject String none Record the message subject
setText String none Record the actual text of the message
getFrom none String Return who the message is from
getTo none String Return who the message is to
getSubject none String Return the message subject
getText none String Return the actual text of the message
getTimestamp none String Return the timestamp indicating when the message was created
send none none Send the email message.

You should have private instance data values for the information that you actually need to store inside a message object.

The MessageMain class

I have provided a class called MessageMain. You should create your own class called MessageMain, and copy and paste in my code. Your Message class must work correctly with my MessageMain class without changing it.

Once you have successfully made my MessageMain class work with your Message class, modify MessageMain so that it uses InputBoxes to prompt the user to indicate who the message is from, who it should be sent to, the subject, and the text of the message itself. It should then send off the email message.

The MailTools class

In order to actually send an email message, you need to use the method "sendMessage" which is part of the class MailTools. MailTools is a class that I created, and so it is in the package called "dmusican". At the top of your Message class, make sure to include:

import dmusican.*;

so that you have access to the MailTools class. The "sendMessage" method has the following header:

public static void sendMessage(String to, String from, String subject, String messageText)

So for example, to send an email from me (at Carleton) to me (at Yahoo), the code would look like

MailTools.sendMessage("musicant@yahoo.com","dmusican@carleton.edu","Test message","Hi -\nThis is a test message.");

The "\n" indicates a new line.

Note that MailTools.sendMessage will only work if you are on a computer on Carleton's campus wired into the Carleton network.

What to Turn In

Turn in your entire BlueJ project, which should contain the classes Message and MessageMain. MessageMain should be the version that you have modified as described above.

Working From Home

In order to work from home for this assignment, you need to install the JavaMail library and the JavaBeans Activation Framework library on your machine. Once you have installed these two libraries, create a folder called dmusican inside your C:\bluej folder. Download MailTools.class and place it inside this folder.

Finally, you need to configure BlueJ to make use of these libraries. Start up BlueJ. Click "Tools", then "Preferences", then click on the "Libraries" tab. Click on the "Add" button, then type in the complete location where JavaMail has included the file mail.jar and the location where JavaBeans Activation Framework has located the file activation.jar. Make sure to include the actual filenames. For example, on my machine at home, I've added the following two lines:

c:\Program Files\JavaSoft\javamail-1.2\mail.jar
c:\Program Files\JavaSoft\jaf-1.0.1\activation.jar

Verify that the line

c:\bluej

appears in here as well.

Shut down BlueJ, and start it up again. Go to the libraries tab again to make sure your changes stuck (occasionally they don't stick, and you have to do it again.) JavaMail and the MailTools class should now work

Other Useful Questions and Answers

Q: How do I obtain the current date and time?
A: Use the Java Date class. Here's an example:

import java.util.*; // at the top of your program

Date builtInDate = new Date();
String date = builtInDate.toString();

Good luck and have fun!!!