|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
Product Reviews Anybody Out There?
Anybody Out There?
By: Alan Williamson
Oct. 1, 1997 12:00 AM
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 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
Sending e-mail via an SMTP server can be broken down into three distinct stages:
Creating a connection 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 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
Using sendEmail
Summary 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. Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||