Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. /**
  33.  *
  34.  * @author      Christopher Cotton
  35.  */
  36.  
  37. import javax.mail.*;
  38.  
  39. /**
  40.  * copier will copy a specified number of messages from one folder
  41.  * to another folder. it demonstrates how to use the JavaMail APIs
  42.  * to copy messages.<p>
  43.  *
  44.  * Usage for copier: copier <i>store urlname</i>
  45.  * <i>src folder</i> <i>dest folder</i> <i>start msg #</i> <i>end msg #</i><p>
  46.  *
  47.  */
  48.  
  49. public class copier {
  50.  
  51.     public static void main(String argv[]) {
  52.         boolean debug = false;  // change to get more errors
  53.        
  54.         if (argv.length != 5) {
  55.             System.out.println( "usage: copier <urlname> <src folder>" +
  56.                                 "<dest folder> <start msg #> <end msg #>");
  57.             return;
  58.         }
  59.  
  60.         try {
  61.             URLName url = new URLName(argv[0]);
  62.             String src = argv[1];       // source folder
  63.             String dest = argv[2];      // dest folder
  64.             int start = Integer.parseInt(argv[3]);      // copy from message #
  65.             int end = Integer.parseInt(argv[4]);        // to message #
  66.  
  67.             // Get the default Session object
  68.  
  69.             Session session = Session.getInstance(System.getProperties(), null);
  70.             // session.setDebug(debug);
  71.  
  72.             // Get a Store object that implements the protocol.
  73.             Store store = session.getStore(url);
  74.             store.connect();
  75.             System.out.println("Connected...");
  76.  
  77.             // Open Source Folder
  78.             Folder folder = store.getFolder(src);
  79.             folder.open(Folder.READ_WRITE);
  80.             System.out.println("Opened source...");      
  81.  
  82.             if (folder.getMessageCount() == 0) {
  83.                   System.out.println("Source folder has no messages ..");
  84.                   folder.close(false);
  85.                   store.close();
  86.             }
  87.  
  88.             // Open destination folder, create if needed
  89.             Folder dfolder = store.getFolder(dest);
  90.             if (!dfolder.exists()) // create
  91.                 dfolder.create(Folder.HOLDS_MESSAGES);
  92.  
  93.             Message[] msgs = folder.getMessages(start, end);
  94.             System.out.println("Got messages...");       
  95.  
  96.             // Copy messages into destination,
  97.             folder.copyMessages(msgs, dfolder);
  98.             System.out.println("Copied messages...");    
  99.  
  100.             // Close the folder and store
  101.             folder.close(false);
  102.             store.close();
  103.             System.out.println("Closed folder and store...");
  104.            
  105.         } catch (Exception e) {
  106.             e.printStackTrace();
  107.         }
  108.  
  109.         System.exit(0);
  110.     }
  111. }
  112.