advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

JavaBoutique : Articles : An Email Component

Contents
Introduction
Introduction to the SMTP Protocol
Creating the Client
Creating a Driver Program and Testing the Client
Converting to Java Version 1.1 or 1.2
Conclusions

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 ->

How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs