Mbox files to an Email server. Such a program is particularly useful for persons who would like to upload their collection of legacy Email messages onto Email servers such as the Gmail server.">
October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Uploading Old Email to Gmail using Java

  • October 18, 2005
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

Complete Program Listings

Complete listings of the programs discussed in this lesson are shown in Listing 20 and Listing 21 below.
 
/*File BigDog05parse.java 
Copyright 2005, R.G.Baldwin
Rev 08/16/05
DISCLAIMER:  THIS PROGRAM IS PROVIDED TO YOU AT 
NO COST.  BY USING THIS PROGRAM TO PROCESS YOUR 
EMAIL, YOU AGREE THAT YOU ARE USING IT AT YOUR 
OWN RISK.  THE AUTHOR OF THE PROGRAM, RICHARD G.
BALDWIN, ACCEPTS NO LIABILITY FOR ANY LOSS THAT 
YOU MAY INCUR THROUGH THE USE OF THIS PROGRAM.
The ultimate purpose of this class is to work in 
conjunction with a class named BigDog05upload to
upload legacy Email messages to an Email server 
such as Gmail.
This program is designed to read an Mbox file 
produced by the Netscape 7.2 Email client and to 
extract and write each msg into an output file 
that is compatible for uploading to an Email 
server using the class named BigDog05upload.
Each output file has a unique file name based on 
the number of milliseconds since Jan 1, 1970.  A
one-millisecond delay is inserted into the 
program between messages to guarantee that no 
attempt is made to write two files within one
millisecond, which would otherwise lead to an
attempt to duplicate file names.  This, in turn,
would lead to some messages being lost.
The following input values are provided as 
command-line parameters:
inputPathAndFile: The path to the Mbox file and
  the name of the file. 
  Example: ./MailToBeProcessed/Inbox
workingDir: Where message files are temporarily
  stored.  Example: ./DataFiles/
destinationAddress: Example: joe@dummy.com
smtpServer: Example: smtp-server.austin.rr.com
uploadTag: A string that is prepended onto the
  Subject line before the message is uploaded.
  Can be an empty string as in "". Example: "ae|"
Tested using JDK 1.5.0_01 under WinXP
************************************************/
import java.io.*;
import java.util.*;
import java.awt.*;
class BigDog05parse{
  //A directory where output message files are
  // stored awaiting uploading by BigDog05upload.
  String workingDir;
  
  //Directory and file name for the Mbox file.
  String inputPathAndFile;
  
  //Save all command-line parameters here
  static String[] params;
  public static void main(String[] args){
    if(args.length != 5){
      System.out.println(
                     "Usage: java BigDog05parse "
                     + "n  inputPathAndFile"
                     + "n  workingDir"
                     + "n  destinationAddress"
                     + "n  smtpServer"
                     + "n  uploadTag");
      System.out.println("Terminating");
      System.exit(0);
    }//end if
    
    //Save and display command-line parameters
    params = args;
    
    System.out.println("inputPathAndFile:   " 
                                    + params[0]);
    System.out.println("workingDir:         " 
                                    + params[1]);
    System.out.println("destinationAddress: " 
                                    + params[2]);
    System.out.println("smtpServer:         " 
                                    + params[3]);
    System.out.println("uploadTag:          " 
                                    + params[4]);
    
    //Instantiate an object of the BigDog05parse
    // class to process the Mbox file.
    new BigDog05parse(params[0],params[1]);
    
    //Messages have been extracted from the Mbox
    // file and saved as individual files.
    // Instantiate an object of the 
    // BigDog05upload class to upload the files
    // to the destination address.
    new BigDog05upload(params[2],
                       params[3],
                       params[1],
                       params[4]);
  }//end main
  //===========================================//
  //Constructor requires inputPathAndFile as
  // first parameter and workingDir as second
  // parameter.
  BigDog05parse(String input,String output){
    this.inputPathAndFile = input;
    this.workingDir = output;
    parseMboxFile();
  }//end constructor
  //===========================================//
  
  void parseMboxFile(){
    String data;
    try{//Get input stream object
      BufferedReader mboxInputObj = 
                          new BufferedReader(
                            new FileReader(
                              inputPathAndFile));
                                  
      DataOutputStream dataOut = null;
      String workingDirAndFile = null;
      String outputFileName = null;
      int msgNumber = 1;
                                  
      //Read and process each line from the
      // Mbox file.
      while((data = mboxInputObj.readLine())
                                        != null){
        if(data.startsWith("From ")){
          //This is the beginning of a new
          // message in the Mbox file. Close the
          // current output file in  order to
          // start a new output file.
          if(dataOut != null){
            dataOut.close();
          }//end if(dataOut != null)
          //Create a unique fileID for the new
          // file name based on the negative
          // value of the current time in ms
          // relative to 1 Jan 1970. Use the
          // negative of the current time instead
          // of the current time to eliminate 
          // leading zeros in the long time
          // value.
          String fileID = Long.toHexString(
                        -(new Date().getTime()));
          //Sleep for 1 ms to guarantee unique
          // file names.
          Thread.currentThread().sleep(1);
          //Open an output file to save the
          // message.  Use the fileID as part of
          // the file name.  Use a file name that
          // is compatible with earlier versions
          // of the programs in the BigDog
          // series.
          outputFileName = "+OK " + 
                        msgNumber + " " + fileID;
          //Concatenate file name to the output
          // path.
          workingDirAndFile =
                     workingDir + outputFileName;
          //Get an output stream for the file.
          dataOut = new DataOutputStream(
                           new FileOutputStream(
                             workingDirAndFile));
          //Show progress
          System.out.print(msgNumber + " ");
          //Increment the message counter in
          // preparation for processing the next
          // message.
          msgNumber++;
        }//end if(data.startsWith("File ")
        else{
          //Write a NewLine at the end of every
          // input line except for the last line
          // in the file.
          dataOut.writeBytes("n");
        }//end else
        //Write the line into the output file.
        dataOut.writeBytes(data);
      }//end while loop
      
      //All messages from the Mbox file have been
      // written into separate files.
      if(dataOut != null){
        //Close the last file.
        dataOut.close();
      }//end if(dataOut != null)
      
      System.out.println();//blank line
      System.out.println(
                      "The Mbox file is parsed");
      System.out.println("Start uploading msgs");
      //Sound an audio alert
      Thread.currentThread().sleep(500);
      Toolkit.getDefaultToolkit().beep();
      Thread.currentThread().sleep(500);
      Toolkit.getDefaultToolkit().beep();
      Thread.currentThread().sleep(500);
    }catch(Exception ex){
      ex.printStackTrace();
      System.exit(0);
    }//end catch
  }//end parseMboxFile
}//end class BigDog05parse
//=============================================//


Listing 20

 

/*File BigDog05upload.java
Copyright 2005, R.G.Baldwin
Rev 08/16/05
DISCLAIMER:  THIS PROGRAM IS PROVIDED TO YOU AT 
NO COST.  BY USING THIS PROGRAM TO PROCESS YOUR 
EMAIL, YOU AGREE THAT YOU ARE USING IT AT YOUR 
OWN RISK.  THE AUTHOR OF THE PROGRAM, RICHARD G.
BALDWIN, ACCEPTS NO LIABILITY FOR ANY LOSS THAT 
YOU MAY INCUR THROUGH THE USE OF THIS PROGRAM.
The ultimate purpose of this class is to work in 
conjunction with a class named BigDog05parse to 
upload legacy Email messages to an Email server 
such as Gmail.
An object of this class is instantiated by the 
program named BigDog05parse to tag and upload a 
set of message files written by the program named
BigDog05parse.    
This object tags messages with a tag passed as a 
constructor parameter.  Then the object uploads 
the messages to a destination email account that 
is specified as a constructor parameter using an 
SMTP server that is specified as a constructor 
parameter.
See additional comments at the beginning of 
BigDog05parse.java.
Tested using JDK 1.5.0_01 under WinXP
************************************************/
import java.io.*;
import java.util.*;
import java.awt.*;
import sun.net.smtp.SmtpClient;
class BigDog05upload{
  //ID of the destination email account.
  final String destinationAddress;
  //An smtp server through which the user is
  // authorized to send email messages.
  final String smtpServer;
  //Local folder where message files are stored
  // awaiting uploading.
  final String workingDir;
  //Tag that is prepended to the Subject line of
  // the message before uploading.
  final String uploadTag;
  //Concatenation of the working directory and
  // the name of a message file.
  String pathFileName;
  //IDs of msg files that will be deleted from
  // the working directory are stored here.
  Vector <String> msgToDelete = 
                            new Vector<String>();
  boolean okToDelete = false;
  int msgNumber = 0;
  //===========================================//
  //Constructor
  BigDog05upload(final String destinationAddress,
                 final String smtpServer,
                 final String workingDir,
                 final String uploadTag){
    this.destinationAddress = destinationAddress;
    this.smtpServer = smtpServer;
    this.workingDir = workingDir;
    this.uploadTag = uploadTag;
    
    System.out.println("Uploading messages:");
    uploadMsgs();
    
    //All messages have been uploaded.
    System.out.println("Deleting message files");
    deleteMsgFiles();
  }//end constructor
  //===========================================//
  
  void uploadMsgs(){
    //The following code creates a directory 
    // listing containing only those files with
    // names that begin with +OK.
    //This is an anonymous implementation of a 
    // class that implements FilenameFilter.
    String[] dirList = new File(workingDir).list(
      new FilenameFilter(){
        public boolean accept(
                           File dir,String name){
          if(!(new File(dir,name).
            isFile())) return false;
          return name.startsWith("+OK");
        }//end accept
      }//end FilenameFilter
    );//end list
    
    if(dirList.length == 0){
      System.out.println("No files to upload");
      System.out.println("Terminating program");
      System.exit(0);
    }//end if
    //Now upload the files in the directory
    // listing.
    for(int msgCounter = 0;
                     msgCounter < dirList.length;
                                   msgCounter++){
      String fileName = dirList[msgCounter];
      pathFileName = workingDir + fileName;

      //This code uploads the message to the
      // destination email account.  The third 
      // parameter can be used to tag the message
      // to show that it was uploaded. Just pass
      // an empty string, "", if you don't want
      // to tag the message.  Return value will
      // be true if the message was successfully
      // uploaded.
      okToDelete = forwardEmailMsg(
                              destinationAddress,
                              smtpServer,
                              uploadTag,
                              pathFileName);
  
      if(okToDelete){
        //Identify the message file for deletion
        // later.
        msgToDelete.add(pathFileName);
        //Display progress
        System.out.print((msgCounter + 1) + " ");
      }//end if
    }//end for loop on directory length
    //Sound an audio alert when all messages have
    // been uploaded.
    try{
      Thread.currentThread().sleep(500);
      Toolkit.getDefaultToolkit().beep();
      Thread.currentThread().sleep(500);
      Toolkit.getDefaultToolkit().beep();
      Thread.currentThread().sleep(500);
      Toolkit.getDefaultToolkit().beep();
      Thread.currentThread().sleep(500);
    }catch(Exception ex){
      ex.printStackTrace();
      System.exit(0);
    }//end catch
    System.out.println();//blank line
    System.out.println("Upload complete");
  }//end uploadMsgs
  //===========================================//
  
  void deleteMsgFiles(){
    //Delete the files in the msgToDelete
    // collection.
    for(int cnt = 0;
                 cnt < msgToDelete.size();cnt++){
      pathFileName = msgToDelete.elementAt(cnt); 
      File file = new File(pathFileName);
      boolean isDeleted = file.delete();
      if(!isDeleted)System.out.println(
             "Unable to delete " + file);
      }//End for loop
  }//end deleteMsgFiles
  //===========================================//
  
  //This method reads and saves lines of data
  // from a file starting with the line that
  // startsWith firstLine and ending with the
  // line that startsWith lastLine.
  //If firstLine is null, data is saved beginning
  // with the first line in the file.
  //If lastLine is null, data is saved to the end
  // of the file.
  //The lines of data from the file are saved by
  // concatenating them into a single string with
  // a newline inserted into the string at the
  // end of each line.
  //The name and path to the file is given by
  // pathFileName.
  public String readLines(String pathFileName,
                          String firstLine,
                          String lastLine){
    StringBuffer strBuf = new StringBuffer();
    try{
      BufferedReader inDataMsg
        = new BufferedReader(new FileReader(
                                  pathFileName));
      String data;
      boolean isSave = false;
      while((data = inDataMsg.readLine())
                                        != null){
        if( ((firstLine == null) ||
             (data.startsWith(firstLine))) &&
             (isSave == false)){
          isSave = true;
        }//end if
        if(isSave){
          strBuf.append(data + "n");
        }//end if
        if((lastLine != null) &&
           (data.startsWith(lastLine))){
          break;//no need to read any more
        }//end if
      }//end while loop
      inDataMsg.close();//Close file
    }catch(Exception e){e.printStackTrace();}
    return new String(strBuf);
  }//end readLines
  //===========================================//
  
  //This method is used to construct an email
  // message and send it to the 
  // destinationAddress.
  public boolean forwardEmailMsg(
                       String destinationAddress,
                       String smtpServer,
                       String uploadTag,
                       String pathFileName){
      StringBuffer message = new StringBuffer(
                             "No message found");
      try{
        //Pass a string containing the name of
        // the smtp server as a parameter to the
        // following constructor.
        SmtpClient smtp =
                      new SmtpClient(smtpServer);
        //Pass any valid email address to the
        // from() method.
        smtp.from(destinationAddress);
        //Pass the email address of the
        // destinationAddress to the to() method.
        smtp.to(destinationAddress);
        //Construct the message as a single
        // StringBuffer object by concatenating
        // all of the lines in the message file.
        message = new StringBuffer(readLines(
                        pathFileName,null,null));
        //Insert uploadTag in subject
        message = message.insert(message.indexOf(
                       "Subject: ")+9,uploadTag);
        //Get an output stream for the message
        PrintStream msg = smtp.startMessage();
        //Write the message into the output
        // stream.
        msg.println(new String(message));
        //Close the stream and send the message
        smtp.closeServer();
        return true;
      }catch( Exception e ){
        e.printStackTrace();
        System.out.println(
                       "while forwarding email");
        //Sound an alarm.
        Toolkit.getDefaultToolkit().beep();
        try{
          Thread.currentThread().sleep(500);
        }catch(Exception ex){
          System.out.println(ex);
        }//end catch
        Toolkit.getDefaultToolkit().beep();
        
        //Return false to indicate that the msg
        // was not successfully forwarded.
        return false;
      }//end catch
  }//end forwardEmailMsg
  //===========================================//
}//end class BigDog05upload
//=============================================//

Listing 21


Copyright 2005, 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 have gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.

In addition to his programming expertise, Richard has many years of practical experience in Digital Signal Processing (DSP).  His first job after he earned his Bachelor's degree was doing DSP in the Seismic Research Department of Texas Instruments.  (TI is still a world leader in DSP.)  In the following years, he applied his programming and DSP expertise to other interesting areas including sonar and underwater acoustics.

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.

Baldwin@DickBaldwin.com





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel