Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Expo on Google News


2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 Event Webcasts
SYS-CON.TV
Top Links You Must Click On


Anybody Out There?
Anybody Out There?

This column looks at the construction of an Intranet-based contact manager known as Informer. In previous articles, I have looked at the building blocks of Informer, and how easy it was to use Symantec's dbAnywhere package to provide all of the database connectivity. For those of you following this column, you will have noted that very little actual code has been produced manually. This is due to Visual Café's excellent drag and drop interface. I could attempt to continue the complete construction of Informer without having to produce a single line of code myself, but that would defeat the purpose of this column: trying to teach Java. And besides that, it would be boring!

So, let's build some features onto our Informer the good old fashioned way: coding. I spend a good part of my day answering other people's problems at both the N-ARY Java forums and Usenet's comp.lang.java.programmer forums, and one of the most common questions that keeps coming up is "How do I send an e-mail from Java?". E-mail functionality from Informer would be a very handy feature to have, so let's kill a couple of birds with one stone and present a self-contained class that may be used to send e-mails.

SMTP
One of the most popular and widely used protocols for sending e-mail over the TCP/IP networks is the Simple Message Transport Protocol (SMTP). You may have noticed this field in your e-mail software where it asks for outgoing host. SMTP hosts come as part of the standard operating system on the majority of UNIX's and for this reason it has widespread acceptance.

A relatively unknown fact about outgoing e-mail is that you can use any SMTP host on the network to deliver your e-mail. Think of an SMTP host like a mailbox sitting on a street corner. You simply write your letter, put an address on it and then place it in the mailbox. At this point, the postal service comes along and collects all the mail and then takes it for processing so it may be delivered. Internet e-mail isn't a million miles away from this analogy. You still write your letter, and you still address it, albeit with an e-mail address (alan@n-ary.com, for example), and then you take it to a mailbox for delivery. An SMTP host is acting in exactly the same manner as the mailbox on the street corner. As long as the message is correctly formatted, it will get delivered.

Although we are free to choose any SMTP host in the world, we are restricted if we are sending e-mail from within a Java applet. then, due to security restrictions we have to ensure that an SMTP host is located on the server where the applet originated. This isn't too much of a concern if the applet is being downloaded from a Web server on a UNIX box. If you are going to send an e-mail from a Java application or Java servlet, then this isn't an issue.

SMTP Communication
Any of you who have written client software to talk to other TCP servers will be familiar with the mechanism required to communicate with SMTP. Like other TCP servers - NEWS, FTP, POP - SMTP uses command strings formatted as ASCII lines and terminated with the carriage return/new line (\r\n) sequence. Depending on the command, the server responds with a status code indicating the success of the previous operation. This status code is formatted as a three-digit number followed by an optional message.

Sending e-mail via an SMTP server can be broken down into three distinct stages:
1. Establishing a connection to the server
2. Sending e-mails
3. Disconnecting from the server

Creating a connection
Creating a connection to an SMTP host involves opening up the network connection and then communicating to the host that we exist. It's at this point that most people new to Java and programming start shaking. Traditionally, network programming has never been easy; anyone who has tried to code an application for Windows using Winsock will testify to that. Java has made network programming child's play.

Connecting to any TCP server can be achieved using the Socket class from the java.net package. Once a new class instance has been created, an input and an output stream is retrieved and communication can begin. For example, look at Listing 1.

Notice how easy it is to create a connection to the host. If for some reason the connection can't be created, then the Socket class throws an exception. This hasn't been shown in the sample code but you will find it in the final listing. Having created the connection, we create two streams, Buffered Reader and DataOutputStream, to handle all the communication.

All communication is formatted as ASCII text; therefore, the class BufferedReader (from JDK1.1) gives us a clean function for reading lines back from the server using the readLine() method. Conversely, sending lines out can be done easily through the DataOutputStream class using the writeBytes(...) method.

Having created a connection, the first thing we will expect from the server is a line that identifies itself. For example:

220 mail.n-ary.com ESMTP Sendmail 8.7.5/8.7.3; Sun, 21 Sep 1997 12:42:00 GMT

The first three digits signify the status code. If we receive a 220 response, we can assume the server is ready and waiting for us. Listing 2 shows how we sit and listen for this status code. We use the indexOf(...) method from the String class, which returns an integer index of the position of the string specified. If the string doesn't exist, then -1 is returned.

Once we receive this, we can send the HELO command which tells the server which domain we are calling from. Again, successful execution of this command will result in a 250 status code being sent back from the server.

Now we are ready to format and send e-mails.

Sending an e-mail
An e-mail has a number of properties but for this example we will assume four of them: originator, recipient, subject and body. This information is sent to the server in a series of short messages.

Sending the originator and recipient of the e-mail is performed using the MAIL FROM: and RCPT TO: commands, formatted with the e-mail address appearing on the same line inside angular brackets (<>). For example, Listing 4 shows sending the server these two commands, and waiting for the respective status codes.

Incidentally, although we have given the functionality here, you can send the same e-mail to multiple users by sending the server repeated RCPT TO: commands. This is particularly useful if you are operating with mailing lists; therefore, you don't need to send the e-mail body x number of times to the server.

Sending the e-mail body to the server is very easy. First, send a DATA command; when the server responds with a 354 status code, you can send the body to the server as a series of lines, terminated with a single dot (.) on a single line. Listing 5 illustrates this.

Notice anything? Before sending the e-mail body, we resent the e-mail originator and sent the Subject of the e-mail inside the body. This information is optional and doesn't need to be present for successful delivery. Document RFC822, the blueprints of the Internet, details the exact format the body text can take. For example, another field that can be added is the mime type of the data. Using this, you can send attachments, and even e-mail formatted as HTML text, that is now becoming a common place in most e-mail clients.

Closing a Connection
After sending the e-mails, connection to the server must be terminated. This is a two-fold process. We need to send the QUIT command to the server and then close the socket connection. No status code is sent from the server, so as soon as we have sent the command we can safely close the socket using the close() method. That's it. Communication finished.

Using sendEmail
We can package the above functionality into a separate class, sendEmail, and use this to send e-mails. I have included the complete listing for this class with this article (see Listing 7). Using the class is very easy. First create an instance of sendEmail, and then call the send(...) method. Listing 6 shows this.

Summary
In our continuing development of Informer, we took a break from the drag and drop building and looked at developing our own class to send e-mail. Although provided as one of the classes in Symantec's library, a custom-built class allows for a greater portability across many Java technologies, for example servlets and beans.

With the e-mail functionality built into Informer, we can look at building a proper user interface to this feature in the next article, while providing some extra features.

About Alan Williamson
Alan Williamson is widely recognized as an early expert on Cloud Computing, he is Co-Founder of aw2.0 Ltd, a software company specializing in deploying software solutions within Cloud networks. Alan is a Sun Java Champion and creator of OpenBlueDragon (an open source Java CFML runtime engine). With many books, articles and speaking engagements under his belt, Alan likes to talk passionately about what can be done TODAY and not get caught up in the marketing hype of TOMORROW. Follow his blog, http://alan.blog-city.com/ or e-mail him at cloud(at)alanwilliamson.org.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Enterprise Open Source Magazine Latest Stories . . .
Apache Deltacloud, the Red Hat-contributed ReSTful API that abstracts differences between clouds so services on any cloud can be managed – provided of course there’s a driver – has graduated from the Apache Foundation’s incubator and is now a full-fledged Top-Level Project (TLP). The...
With Cloud Expo 2012 New York (10th Cloud Expo) just four months away, what better time to start introducing you in greater detail to the distinguished individuals in our incredible Speaker Faculty for the technical and strategy sessions at the conference... We have technical and st...
AMD said late Tuesday that its chief sales officer Emilio Ghilardi had left the company and that CEO and president Rory Read is going to do his job while a replacement is sought. AMD didn’t say why Ghilardi left but it’s assumed Read wants his own people. Read is relatively new to th...
During the lifespan of M3 (Monitis Monitor Manager) there has always been something lacking – timers. M3 execution procedure was outlined in this previous article. The execution mentioned in the latter was a one-time-execution, whereas server monitoring requires periodic invocati...
Red Hat is putting its bought-in Gluster scale-out NAS storage technology, acquired in October, on the Amazon cloud. It’s styled Red Hat Virtual Storage Appliance for Amazon Web Services and other clouds are supposed to follow in short order.
A new episode of the screencast series is now available at the OpenNebula YouTube Channel. This screencast demonstrates the new easily-customizable self-service portal for cloud consumers. Its aim is to offer a simplified access to shared infrastructure for non-IT end users. The scree...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON Featured Whitepapers
ADS BY GOOGLE