Creating a Driver Program and Testing the Client
Portions of this section refer to the source file emcdriver.java.
To compile the source file, use
javac -deprecation emcdriver.java
This will compile the source to create emcdriver.class. You can
then use appletviewer to start the driver so you can send an email (emcdriver
is embedded in emc.html)
AppletViewer emc.html
When you first use the driver, you should put the SMTP server you usually
use (ie the one that you use with Netscape email client or Outlook or your
preferred email client) into the server text field, then fill out the fields
and send (use real email addresses, its best to use your own to begin with).
You can try other servers, but you will usually get an error message which
indicates that relaying is not permitted. You should leave the "Use Applet
Constructor" checkbox unchecked, its use is discussed later.
If you want to see an error message, use an SMTP server other than the
own you usually use to send messages, it should give you a no relay allowed
message.
The init method merely creates components and lays them out. For
the sake of appearance, a gridbag is used. This class primarily just
creates an interface and responds to button clicks.
The important code is the event handling in the action method.
And the key lines are the following:
if(cbUseApplet.getState())
mailer=new emc((Applet)this, textEmailFrom.getText(), textEmailTo.getText(),
textNameFrom.getText(),
textNameTo.getText(), textSubject.getText(), ta.getText());
else
mailer=new emc(textServer.getText(), textEmailFrom.getText(),
textEmailTo.getText(),
textNameFrom.getText(), textNameTo.getText(), textSubject.getText(), ta.getText());
If the "Use Applet Constructor" checkbox was checked, the applet is
cast to Applet (if this is an applet, the Applet class with be the superclass),
otherwise the SMTP server name is passed to emc. The rest of the
information is also passed in via the constructor, including the sender
and recipient's names and email addresses, the subject, and the content
of the message.
If you have an HTTP server and SMTP server running on your local system
(and who doesn't? :) ), you can try the applet constructor. Download
the applet into a browser from localhost, then use the applet to send mail
via the SMTP server running on local host. You can also try this
on the web, if the website you download from is from your local ISP, you
may not have a problem with SMTP relay filtering, otherwise you will get
an error message about relay not permitted (error messages are displayed
in the top label).
After the mailer object is created, a boolean called bSending is set
to true. When the mail is sent (checked during the polling loop)
bSending is set to false and the emailer can be used again. The emailer
could be re-written to send multiple messages by using a vector and adding
new elements as new messages were sent and removing elements that had finished
sending.
Any information that can be presented as text can be transferred via
the emc class (for an example, see the ascii art editor at: http://www.lithic.com/java/ili.html,
it can email ascii art, when downloaded from a server that permits relaying
(the server that it is on does not)).
The way that the emc class passes information is by using public variables.
These variables can be checked by the class that instantiated emc, either
using a polling loop, or by checking them in response to an event (for
example, checking to see whether sending is done when the user wants to
exit a program).
For classes where there are only a few possible states, and where there
is a definite point when the transaction is finished, using public variables
works fairly well. For more complicated classes that are running
in their own thread, the Java 1.1 event model is a much better choice,
but use in applets will be limited.
NEXT ->
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|