Java Programming Notes # 2182
- Preview
- Operational Discussion
- Program Code
- Run the Programs
- Summary
- What's Next?
- Complete Program Listing
Preview
The previous lesson entitled Enlisting Java in the War Against Email Viruses described several of the significant dangers you face when you allow email-borne viruses to get into the email database on your computer. That discussion ended with the conclusion that email-borne viruses can be far more serious than just being a nuisance.
Not only can they cause you to lose important business email messages, they can also cause you to lose a wide-ranging variety of business information in the event of a need to restore business data from backup files.
Even worse, if an email-borne virus gets loose in your computer, it can do irreparable harm to your computer and many other computers as well.
No virus messages allowed
The best thing is to trap and destroy all email-borne viruses before they have an opportunity to contaminate the email database on your computer.
The purpose of this lesson, and the previous one as well, is to show you one way to accomplish that goal.
A simple and inexpensive scheme
The scheme that I described in the previous lesson is very simple and also very inexpensive. The resources required consisted of three computer programs and two email accounts, one public and one private. Two of the computer programs are free, because I am providing them to you. The third computer program, a virus scanner, is a program that you should already have anyway.
The scheme that I will describe in this lesson is even simpler and less expensive. This scheme doesn't require a second email account. Furthermore, the program that I will provide in this lesson will run much faster than the program provided in the previous lesson.
Not as general
However, the program that I will provide in this lesson is not as general as the one provided in the previous lesson. That program was designed to be used with any POP3 server and any email client program.
The program that I will provide in this lesson is also designed to be used with any POP3 email server, but can only be used with email client programs that employ a common email storage format known as MBOX for local storage of messages.
Many email client programs use MBOX
I have read that many email client programs use MBOX format for local storage of messages. Most notable among them is the email client in Netscape version 7.0 and later. (I believe that earlier versions of Netscape also use MBOX, but I have no way to confirm that.)
Unfortunately, I have also read that Microsoft Outlook does not use MBOX format for local storage of messages. Therefore, this program is probably not compatible with Outlook in its current form.
(Perhaps someone who knows more about Outlook that I do will provide a modified version that is compatible with the message storage format of Outlook.)Two of the programs are free
As mentioned above, two of the required programs are free, because I am publishing them in this and the previous lesson. The third required program is a good virus checker program, which you should already have anyway.
The operational scheme
The operational scheme is very simple. I described it in detail in the previous lesson, and will recap it here with changes pertinent to this version of the program.
For now, let's call the two programs that I will provide program A and program C.
No changes to program A
There have been no significant changes to program A since the publication of the previous lesson. Program B, as described in the previous lesson has been replaced by program C in this lesson.
Therefore, the first two-thirds of the operational procedure is still the same as described in the previous lesson.
Step 1: Download messages from your public email server
To begin with, you never use your email client program to download email messages from your public email account. Rather, you run program A to download the messages for you.
A separate file for each message
When you run program A, it will download all of the messages on the server and write each of those messages into a separate file in a specified folder on your disk. At this point, the messages are still separated from one another. A virus in one message cannot corrupt another message.
(On my system with a cable modem, I can download a batch of one hundred messages in just a few seconds, so this process is very fast.)Step 2: Scan the message files for viruses
Next you use your favorite virus checker program to scan all of the message files produced by program A, either cleaning or removing any that contain a virus.
(My advice would be to totally remove the contaminated file to avoid any possibility of creating an invalid message format in the file. )On my system, which is relatively slow, only a few seconds are required to scan a batch of one hundred messages.
Step 3: Write an MBOX file containing the clean messages
The next step is to run program C. This program combines all of the clean messages into a single MBOX file and deposits that file in the directory tree belonging to your email client program.
If you did the virus check (and if you keep your virus checker program up to date), all of these messages should be free of viruses.
A unique file name
The file is given a unique name based on the current date and time, prefixed with an upper case A. The next time you start your email client program, this file will appear as a folder in your email client display. That folder will contain all of the messages that were combined into the MBOX file.
A dummy email account may be useful
You can either cause the MBOX file to be stored in the directory tree for your primary email account, or you can establish a dummy email account and store it there if your email client program provides support for multiple accounts. It really doesn't matter, so long as the name of the MBOX file does not duplicate one of your existing email folder names.
(This is highly unlikely because the file name contains a long string of digits, but it would be even less likely with a dummy email account that doesn't contain any other working folders.)A very fast process
This process is very fast. Only a couple of seconds are required on my relatively slow machine to process a batch of approximately one hundred messages.
As soon as this process completes, you can restart your email client program and begin reading the messages contained in the new folder.
Worth the extra time and effort
Unless you have a very large number of messages, a very slow computer, or a very slow Internet service, the steps required before you can start reading your messages should take no more than fifteen or twenty seconds to complete. This is a small price to pay for assurance that your email database is free of viruses.
Viewing tip
You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the different listings and figures while you are reading about them.
Supplementary material
I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find those lessons published at Gamelan.com. However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there. You will find a consolidated index at www.DickBaldwin.com.
Operational
Discussion
The operation of each of the two programs is very simple. The
names of the two programs are VirPro01a and VirPro01c,
corresponding respectively to program A and program C in the
hypothetical case discussed above.The program named VirPro01a
As mentioned earlier, the program named VirPro01a has not changed since the publication of the previous lesson. I discussed this program in detail in the previous lesson, and won't repeat that discussion here.
Run the virus checker
After VirPro01a finishes downloading the messages and storing them in individual files, the next step is to run your virus checker on all the files in the working folder, removing any that contain a virus.
Starting the program named VirPro01c
Once you have a compiled version of VirPro01c on your system, and you have created an archive directory in addition to the working directory, you start the program named VirPro01c by entering the following at the command line prompt.
java VirPro01c pubServer userName password
In this case, the command line parameters identify:
- Your public email server
- Your user name on the public server
- Your password on the public server
What is this information used for?
This information makes it possible for the program to delete the corresponding messages from the public email server once they have been written into the MBOX file.
(You will probably want to delete the message files from the archive folder periodically to free up disk space.)Why have an archive folder?
With my setup, I tag the subject line of each message written into the MBOX file with the sequential message numbers from the public email server. (You can modify the program to substitute any tag that you choose, including no tag at all.) Tagging with message numbers makes it easy for me to confirm that every message arrived safely in my email client program folder.
If a message doesn't show up ...
In the unlikely event that a message fails to show up, there is a backup copy in the archive folder. You can view the backup copy with an ordinary text editor. I discussed the various ramifications of viewing a raw message with a text editor in the previous lesson.
Keeping a backup copy of the message
In any event, I consider it a good idea to keep a backup copy of the message files for a couple of days after writing them into the MBOX file, just in case I need them for some reason. You can easily delete this feature if you don't consider it to be important.
The user interface for VirPro01c
Figure 1 shows the simple user interface for VirPro01c.

Figure 1 User interface for VirPro01c
The user presses the Start button to cause all the messages in the working folder (produced by VirPro01a and scanned by your favorite virus scanner program) to be added to the MBOX file.
The screen shot in Figure 1 was captured at the point where all the messages in the working folder had been written into the MBOX file, and the program was requesting permission to delete those messages from the public email server.
As you can see, once all of the messages in the working directory have been written into the MBOX file, the Delete button is enabled. This makes it possible for the user to delete the messages from the public email server, and to cause the corresponding message files to be moved from the working folder to the archive folder.
Program Code
Program VirPro01aAs I mentioned earlier, I explained the program named VirPro01a in the previous lesson, and won't repeat that explanation here.
Program VirPro01c
This program contains quite a bit of new information relative to the program named VirPro01b, which I explained in the previous lesson.
As described earlier, this is one of a pair of programs designed to be used together, in conjunction with a good virus scanner program to prevent messages with viruses from contaminating your email database.
This program should be run after the program named VirPro01a has been run, and a virus checker has been used to confirm that the files produced by VirPro01a are free of viruses. Any files that contain viruses should be removed from the working folder.
This program writes the remaining messages into an MBOX file and deposits that file into the directory structure belonging to your local email client program.
On my system, which is relatively slow, only a few seconds are required to put a batch of about one hundred messages into the MBOX file and to write it into the correct location.
This program was tested using SDK 1.4.2 under WinXP.
The addToMboxStr method
The version of the program discussed in the previous lesson contained a rather complex method named forwardEmailMsg. One of the major changes to this program (relative to VirPro01b) is the replacement of the forwardEmailMsg method by a new method named addToMboxStr.
As the name implies, the addToMboxStr method is used to add a message to a String containing previously-added messages. This method is somewhat less complex than the forwardEmailMsg method, but still contains some interesting new material. I will begin my discussion of the program code with a method that is called by the addToMboxStr method.
The readLines method
The addToMboxStr method calls another method named readLines. I explained that method in detail in the previous lesson and won't repeat those details here.
Basically, the method allows you to extract consecutive lines of text from a text file based on the starting content of the first line and the last line that you want to capture.
The lines of text from the file are saved by concatenating them into a single String object with a newline inserted into the string at the end of each line.
Now back to the addToMboxStr method
The addToMboxStr method begins in Listing 1
private StringBuffer addToMboxStr( |
- mBoxStrBuf: A StringBuffer object, potentially containing messages previously added, and to which the new message is to be appended.
- tag: A String that will be inserted into the Subject line immediately before the body of the Subject line. My version provides the original message number on the public email server as a tag, but you can modify this to be anything that you find useful.
- pathFileName: A string containing the path and file name of the file containing the raw message of interest.
Get and save the raw message
Listing 2 invokes the readLines method discussed earlier to get the raw message text as a single String object with new line characters inserted to separate each of the text lines in the original message.
StringBuffer message = new StringBuffer( |
Prepare the message for MBOX compatibility
Preparation of the message for MBOX compatibility requires the creation of four new header lines and the insertion of those four lines at the beginning of the message. Examples of the four new header lines follow:
- From - Wed Jan 21 15:59:09 2004
- X-UIDL: 400ed7770000000b
- X-Mozilla-Status: 0000
- X-Mozilla-Status2: 00000000
The second line contains the UIDL from the email server.
The meaning of the third and fourth lines can be found at various web sites including http://www.eyrich-net.org/mozilla/X-Mozilla-Status.html.
For a new message, the two status values given above are satisfactory.
Insert Mozilla header lines
Listing 3 creates the four special header lines and inserts them at the beginning of the message as required.
//Get a 24-char date string matching |
Create a blank line at the end of the message
Listing 4 appends a blank line at the end of the message as required by the MBOX format described in the above reference.
message.append("\n"); |
Listing 5 inserts the tag in the Subject line of the message at the beginning of the body of the Subject line (following Subject: ).
message = message.insert(message.indexOf( |
Listing 6 appends the message to the StringBuffer object that will ultimately be used to create the MBOX-formatted file.
mBoxStrBuf.append(message); |
Listing 6 also signals the end of the method named addToMboxStr.
The moveFile method
The program shown in Listing 16 near the end of the lesson contains a method named moveFile, which will be invoked later.
This method was not modified in the transition from the program named VirPro01b to the program named VirPro01c. This method was discussed in detail in the previous lesson and won't be discussed further here.
The validateOneLine method
Similarly, Listing 16 contains a method named validateOneLine, which has not changed relative to the program named VirPro01b, which was discussed in the previous lesson. Therefore, I also won't discuss this method in this lesson.
That brings us to a discussion of the main class for this program.
The VirPro01c class
This class is a modification of VirPro01b class discussed in the previous lesson.
Purpose
The purpose of this modified class is to create and to populate an MBOX file directly in the email tree structure on the local disk. This is an alternative to forwarding the messages to a secret email account via the Internet.
Operational speed and generality
Elimination of the Internet forwarding operation causes this program to run much faster than VirPro01b.
As mentioned earlier, however, this version is less general than the previous version.
While the previous version was compatible with essentially any email client program, this version is compatible only with email client programs that store messages locally in files using a format known commonly as MBOX.
The email client built into Netscape 7.0 and later versions is one email client program that uses MBOX format.
One of a pair of programs
This is one of a pair of programs designed to be used together, in conjunction with a good virus scanner program, to prevent messages with viruses from contaminating your email inbox. The other program in the pair is the program named VirPro01a, which was discussed in the previous lesson.
VirPro01c should be run after the program named VirPro01a has been run, and after a good virus scanner program has been used to confirm that all the files produced by VirPro01a are free of viruses. Any files containing viruses should be removed from the system before VirPro01c is run.
Writing and populating the MBOX file
This program writes email messages into an MBOX file, which is located within the directory tree belonging to an email client program.
Only a few seconds are required to process a batch of approximately one hundred messages and to write them into the MBOX file. This is much faster than the program named VirPro01b, discussed in the previous lesson, can forward messages to a secret email account via the Internet.
Unique MBOX file names
Each time the program is run, a new MBOX file with a unique file name is created and populated with the messages previously created by VirPro01a and scanned with your favorite virus scanner program.
The name of the MBOX file is based on the current date and time prefixed with an upper case A. This file appears as a folder in your email client program display.
Terminate your email client program
You should terminate your email client program before running VirPro01c. If you don't, the email client program may not recognize the new email folder corresponding to the MBOX file until the email client program has been stopped and then restarted.
Designed to work with POP3 server
When requested to do so by the user, this program deletes messages from the user's email server. The messages that are deleted are the same messages that have been written into the MBOX file.
The program is designed to be used with a POP3 email server for deletion of messages. For technical information on POP3, see RFC 1725 at http://www.cis.ohio-state.edu/htbin/rfc/rfc1725.html.
Usage instructions
To start the program, enter the following at the command line:
java VirPro01c pubServer userName password
where the command-line parameters identify:
- Your public email server
- Your user name on the public server
- Your password on the public server
Listing 7 shows the beginning of the VirPro01c class along with the declaration and initialization of some instance variables.
class VirPro01c extends Frame{ |
The emailPath variable
This is the path to the local folder where you write files to cause them to be treated as email folders by your email client program.
Not necessarily a valid email account
This doesn't have to be a valid email account. In other words, if your email client program allows you to set up multiple email accounts, you can set this account up using a dummy POP3 server name, dummy account name, etc. You will never need to cause your email client program to make use of the POP3 server and account names.
A repository for filing email messages
This account serves simply as a repository for email folders allowing you to file your email messages in an orderly manner. The messages enter your email database by way of the program named VirPro01c.
Once a new folder has been created and populated by VirPro01c, you should start your email client program and process the messages contained in that folder.
(The email folder name matches the unique file name produced by VirPro01c.)Processing your messages
Normally you will want to view the subjects and the senders of messages in the new folder and either delete them, or drag them to other folders that you have established for orderly filing of your messages.
Once you have emptied a folder produced by VirPro01c, you should use the facilities of your email client program to delete that folder. It will be of no further use.
No reason for email client program to support POP3
In the final analysis, there is no reason that you must set your email client program up so that it can actually communicate with your POP3 email server.
When operating according to the methodology described herein, you will always download your email messages using the program named VirPro01a, scan them with your favorite virus scanner program. Then you will move the clean messages into your email client program's database using the program named VirPro01c.
You will need SMTP capability
Of course, you will need for your email client program to be properly set up for your SMTP server so that you can send email messages.
The main method
The main method for the VirPro01c class is shown in Listing 8.
public static void main(String[] args){ |
The constructor
The constructor for the class named VirPro01c begins in Listing 9.
VirPro01c(final String server, |
Register a WindowListener and declare component references
The code in Listing 10 is identical to the code discussed in the previous lesson and won't be discussed further here.
this.addWindowListener( |
Register an ActionListener on the startButton
Listing 11 shows the beginning of the code required to register an ActionListener object on the startButton.
//Register an ActionListener on the |
Add the message to the MBOX file
Listing 12 shows the point where this program named VirPro01c departs significantly from the code in the program named VirPro01b discussed in the previous lesson.
mBoxStrBuf = addToMboxStr(mBoxStrBuf, |
The method named addToMboxStr was discussed earlier in this lesson. You can tag the subject with any string that you choose to pass as the second parameter to the addToMboxStr method. I have chosen to tag the subject with a three-digit message number representing the original message number on the email server. That makes it easy to confirm that all messages successfully reached the email account.
Indicate that the message has been handled
Listing 13 posts a message on the GUI in Figure 1 showing that the message has been added to the MBOX file.
textArea.append("Added " + |
Listing 13 also signals the end of the loop used to process all of the message files produced earlier by the program named VirPro01a.
Write the MBOX file
Listing 14 writes the String object containing the MBOX-formatted messages into the output file.
try{ |
Complete the definition of the ActionListener
Listing 15 completes the definition of the ActionListener being registered on the Start button in Figure 1.
deleteButton.setEnabled(true); |
- Enable the Delete button in Figure 1 to make it possible for the user to delete messages from the email server.
- Post a message in the text area of Figure 1 regarding deletion of messages.
- Provide an audible alert to the user.
Remaining code
The remaining code in the VirPro01c class is shown in Listing 16 near the end of the lesson. This code is the same as that discussed in the class named VirPro01b in the previous lesson. Therefore, I won't discuss it further in this lesson.
The virus message is still on the server
There is one more issue that I need to discuss. If your virus scanning program detects a virus in a downloaded message file and removes that file from your computer, the file won't be processed by these programs. That is exactly what you want to happen. However, because the contaminated file won't be processed locally, it also won't be deleted from the email server. Therefore, you need some other way to delete the contaminated message from the server.
Move to the head of the line
If all of the other messages in the current batch of messages are successfully deleted from the server, the message containing the virus should move to the position of message number 1 on the server.
(You can confirm that by running these two programs again. The message containing the virus will be downloaded again, and your virus scanning program should once again identify the file containing the virus. The file name contains the message number immediately following the +OK in the file name.)Removing the contaminated message from the server
If you have web mail access to your email server, the easiest thing to do at this point is to access the server using web mail and to delete the message directly from the server. That is what I do.
(Although my web mail service doesn't expose the message number for each message, it does visually identify the messages that have previously been downloaded but have not yet been deleted. That makes it easy for me to find and delete the contaminated message from the server.)What if you don't have web mail?
In this case, you will need a special program designed specifically to delete the contaminated message from the server.
Since the purpose of these lessons is to help you learn to program using Java, I am not going to provide that program for you. Rather, this is a relatively simple program, so I am going to let you write it for yourself.
If you understand the programs discussed in this and the previous lesson, it shouldn't be a great challenge for you to use the concepts embodied in those programs and to write a new program that connects to your email server and deletes message number 1.
(Before doing that, you should confirm that the contaminated message really does move to the position of message number 1 on your system by cycling through these programs two or three times and observing the message number in the name of the contaminated message file.)
Run the Programs
I encourage you to copy the code from Listing 40 in the previous
lesson and Listing 16
below. Compile and run the programs. Experiment with them,
improving them as you see fit relative to your specific situation.
(IMPORTANT: Let me caution you not to enable the DELE code in the program named VirPro01c until you are certain that you actually want to delete messages from the server. Once a message is deleted from the server, there is no way to recover it from the server.)
Summary
This lesson provides a Java program and an operational scheme for preventing email viruses from contaminating your email data.Preventing email data contamination can be very important, not only with respect to protecting valuable email correspondence, but also with respect to ensuring that you have backups suitable for restoring your disk data when needed.
What's Next?
I have no immediate plans for publishing additional lessons on this topic. However, my long-term plans call for combining this scheme with a SPAM screening scheme for the purpose of protecting against email viruses and blocking SPAM with the same program.Complete Program Listing
Disclaimer of responsibility: If you elect to use these programs you use them at your own risk. Make absolutely certain that you understand what you are doing before you compile and execute the programs. Inappropriate use could result in the loss of email messages. The author of these programs, Richard G. Baldwin, accepts no responsibility for any losses that you may incur as a result of using these programs.
/*File VirPro01c.java |
Copyright 2004, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
About the author
Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.Richard has participated in numerous consulting projects, and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Programming Tutorials, which has gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
-end-