Creating the Client
To create the client from the source code, compile emc.java. From
the command line using the JDK you can compile as follows:
javac -deprecation emc.java
This will create the class file that can be used to send email.
Two constructors are used, one is for applications, or applets that
are not running in a browser, the other is for use with applets running
in a browser. The reason for this is that applets running in a browser
generally have the security restriction that they can only connect back
to the server from which they were downloaded. Because of this, it
is easier for the programmer incorporating the emc class into their applet
to use "(Applet)this" in their code as the first parameter passed to the
constructor.
Examining the source of emc.java, you will note that the class extends
Thread, this is so that it returns after being started, so that the instantiating
class does not have to wait for it to finish. The last line of the
constructor starts the thread in which the class will run. When the
run method is called, the class starts to send the information that was
passed in via the constructor. The conversation occurs in accordance
with RFC 821. Messages returned by the server are checked for error
codes on each transaction.
The first step in the run method is to create a socket that is connected
to an SMTP server (generally SMTP servers listen on port 25). The
code either attaches to the server that the applet was downloaded from,
or to the host that was specified in the constructor used by applications
and applets running in AppletViewer.
if (bApplet)//if the applet constructor is used, this is set to true
MailSocket=new Socket(parent.getCodeBase().getHost(), 25);
else
MailSocket=new Socket(host, 25);//used for applications
and applets running in AppletViewer
The parent.getCodeBase().getHost() function returns the host name as
a string, for example, if the applet was downloaded from lithic.com, this
function would return "lithic.com". This socket constructor (there
are several) takes a string as the first parameter and an int as the second.
If you were hard coding and wanted to create a connection to "mail.someserver.com",
you would use:
new Socket("mail.someserver.com", 25);
Code is included that catches exceptions during the creation of the
socket. The unknown host exception indicates that there was no DNS
entry for the host. IO Errors and any other exception are then trapped.
If an error is detected, the thread stops and the method returns.
Once the socket is created, PrintStream and DataInputStream objects
can be created so that writing to and reading from the socket can each
be done with calls to single methods. Note that methods in the PrintStream
class never throw I/O exceptions, so none are caught in the code for println.
Errors for an instance of PrintStream can be checked using the checkError
method (not done in this code).
try{OutputStream = new PrintStream(MailSocket.getOutputStream());}
catch (java.io.IOException e) { ioError(); return;}
catch (Exception e) { otherError(); return;}
try{in=new DataInputStream(MailSocket.getInputStream());}
catch (java.io.IOException e) { ioError(); return;}
catch (Exception e) { otherError(); return;}
Every time a response is read from the server, the response is analyzed
for error codes (discussed at more length later). If an error code
is found, the thread stops.
A typical conversation between an SMTP server and the emc client follows.
Note that the sending of the information terminates when the client sends
a line that contains only a single "." . The println method terminates
each line with a <CR><LF> (which is required by the protocol).
SMTP Server: 220 SMTP Server mail.lithic.com (Not a real smtp
server)
emc
: HELO emc
SMTP Server: 250 hello mail.lithic.com
emc
: MAIL FROM:<keo@lithic.com>
SMTP Server: 250 ok
emc
: RCPT TO:<webmaster@javaboutique.com>
SMTP Server: 250 ok, its for <webmaster@javaboutique.com>
emc
: DATA
SMTP Server: 354 ok, send it; end with <CRLF>.<CRLF>
emc
: Date: 1 Aprlil 2000 15:39:45
emc
: From: John Keogh <keo@lithic.com>
emc
: To: Webmaster <webmaster@javaboutique.com>
emc
: Subject: emc Article
emc
:
emc
: Scott,
emc
: A lot of people were requesting source, so I decided to write this
article.
emc
: Please let me know what you think...
emc
: John
emc
:.
SMTP Server: 250 Message queued
emc
: QUIT
Note the period on the third to last line. The period terminates
the send of the data. QUIT terminates the session. All of the
information required for the conversation is passed in through the constructor
(ie email addresses, names, subject, and body).
The code that checks whether the return was an error simply checks to
see if the return code (the first 3 characters of the server response)
was greater than or equal to 400, which indicates an error. If the
return code indicates an error, the thread stops, the public variables
strError and iError are set to the response sent by the server and the
error code, respectively. The boolean bDone is set to true and the
thread stops. When the class that instantiated emc checks,
it will find the boolean bDone is true, that the sending failed because
iError is not equal to NONE, and can display the error string returned
by the host. If the error was an io or unknown host exception, the
error string and code are set appropriately in the associated function.
This enables diagnostics by the class that instantiated emc. Note
that it can take a long time for the method calls to fail. In which
case the user may not get any useful information for some time. There are
more error codes now than those that were described in the original RFC,
however the error codes are all >400, and the string returned by the server
is available to the class that instantiates emc, making it very flexible.
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.
|