KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > copier


1 /*
2  * @(#)copier.java 1.9 03/04/22
3  *
4  * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 /**
40  *
41  * @version 1.9, 03/04/22
42  * @author Christopher Cotton
43  */

44
45 import javax.mail.*;
46
47 /**
48  * copier will copy a specified number of messages from one folder
49  * to another folder. it demonstrates how to use the JavaMail APIs
50  * to copy messages.<p>
51  *
52  * usage for copier: copier <i>protocol</i> <i>host</i> <i>user</i>
53  * <i>password</i> <i>src folder</i> <i>dest folder</i> <i>start msg #</i> <i>end msg #</i><p>
54  *
55  */

56
57 public class copier {
58
59   public static void main(String JavaDoc argv[]) {
60       boolean debug = false;// change to get more errors
61

62       if (argv.length != 5) {
63       System.out.println( "usage: copier <urlname> <src folder>" +
64                   "<dest folder> <start msg #> <end msg #>");
65       return;
66       }
67
68       try {
69       URLName url = new URLName(argv[0]);
70       String JavaDoc src = argv[1]; // source folder
71
String JavaDoc dest = argv[2]; // dest folder
72
int start = Integer.parseInt(argv[3]); // copy from message #
73
int end = Integer.parseInt(argv[4]); // to message #
74

75       // Get the default Session object
76

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

80       // Get a Store object that implements
81
// the protocol.
82
Store store = session.getStore(url);
83       store.connect();
84       System.out.println("Connected...");
85
86       // Open Source Folder
87
Folder folder = store.getFolder(src);
88       folder.open(Folder.READ_WRITE);
89       System.out.println("Opened source...");
90
91       if (folder.getMessageCount() == 0) {
92         System.out.println("Source folder has no messages ..");
93         folder.close(false);
94         store.close();
95       }
96
97       // Open destination folder, create if needed
98
Folder dfolder = store.getFolder(dest);
99       if (!dfolder.exists()) // create
100
dfolder.create(Folder.HOLDS_MESSAGES);
101       System.out.println("Opened dest...");
102
103       Message[] msgs = folder.getMessages(start, end);
104       System.out.println("Got messages...");
105
106       // Copy messages into destination,
107
folder.copyMessages(msgs, dfolder);
108       System.out.println("Copied messages...");
109
110       // Close the folders and store
111
folder.close(false);
112       dfolder.close(false);
113       store.close();
114       System.out.println("Closed folders and store...");
115       
116       } catch (Exception JavaDoc e) {
117       e.printStackTrace();
118       }
119
120       System.exit(0);
121   }
122
123 }
124
125
126
Popular Tags