FileDocCategorySizeDatePackage
copier.javaAPI DocGlassfish v2 API4037Fri Jul 14 17:35:18 BST 2006None

copier

public class copier extends Object
copier will copy a specified number of messages from one folder to another folder. it demonstrates how to use the JavaMail APIs to copy messages.

usage for copier: copier protocol host user password src folder dest folder start msg # end msg #

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] argv)

      boolean debug = false;// change to get more errors
      
      if (argv.length != 5) {
	  System.out.println( "usage: copier <urlname> <src folder>" +
			      "<dest folder> <start msg #> <end msg #>");
	  return;
      }

      try {
	  URLName url = new URLName(argv[0]);
	  String src = argv[1];	// source folder
	  String dest = argv[2];	// dest folder
	  int start = Integer.parseInt(argv[3]);  // copy from message #
	  int end = Integer.parseInt(argv[4]);	// to message #

	  // Get the default Session object

	  Session session = Session.getInstance(System.getProperties(), null);
	  // session.setDebug(debug);

	  // Get a Store object that implements 
	  // the protocol.
	  Store store = session.getStore(url);
	  store.connect();
	  System.out.println("Connected...");

	  // Open Source Folder
	  Folder folder = store.getFolder(src);
	  folder.open(Folder.READ_WRITE);
	  System.out.println("Opened source...");	  

	  if (folder.getMessageCount() == 0) {
		System.out.println("Source folder has no messages ..");
		folder.close(false);
		store.close();
	  }

	  // Open destination folder, create if needed 
	  Folder dfolder = store.getFolder(dest);
	  if (!dfolder.exists()) // create
	      dfolder.create(Folder.HOLDS_MESSAGES);

	  Message[] msgs = folder.getMessages(start, end);
	  System.out.println("Got messages...");	  

	  // Copy messages into destination, 
	  folder.copyMessages(msgs, dfolder);
	  System.out.println("Copied messages...");	  

	  // Close the folder and store
	  folder.close(false);
	  store.close();
	  System.out.println("Closed folder and store...");
	  
      } catch (Exception e) {
	  e.printStackTrace();
      }

      System.exit(0);