Debian Codegraph @

demo/src/main/java/copier.java

raw
1 /*
2 * Copyright (c) 1996, 2020 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0, which is available at
6 * http://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10
11 /**
12 *
13 * @author Christopher Cotton
14 */
15
16 import jakarta.mail.*;
17
18 /**
19 * copier will copy a specified number of messages from one folder
20 * to another folder. it demonstrates how to use the Jakarta Mail APIs
21 * to copy messages.<p>
22 *
23 * Usage for copier: copier <i>store urlname</i>
24 * <i>src folder</i> <i>dest folder</i> <i>start msg #</i> <i>end msg #</i><p>
25 *
26 */
27
28 public class copier {
29
30 public static void main(String argv[]) {
31 boolean debug = false; // change to get more errors
32
33 if (argv.length != 5) {
34 System.out.println( "usage: copier <urlname> <src folder>" +
35 "<dest folder> <start msg #> <end msg #>");
36 return;
37 }
38
39 try {
40 URLName url = new URLName(argv[0]);
41 String src = argv[1]; // source folder
42 String dest = argv[2]; // dest folder
43 int start = Integer.parseInt(argv[3]); // copy from message #
44 int end = Integer.parseInt(argv[4]); // to message #
45
46 // Get the default Session object
47
48 Session session = Session.getInstance(System.getProperties(), null);
49 // session.setDebug(debug);
50
51 // Get a Store object that implements the protocol.
52 Store store = session.getStore(url);
53 store.connect();
54 System.out.println("Connected...");
55
56 // Open Source Folder
57 Folder folder = store.getFolder(src);
58 folder.open(Folder.READ_WRITE);
59 System.out.println("Opened source...");
60
61 if (folder.getMessageCount() == 0) {
62 System.out.println("Source folder has no messages ..");
63 folder.close(false);
64 store.close();
65 }
66
67 // Open destination folder, create if needed
68 Folder dfolder = store.getFolder(dest);
69 if (!dfolder.exists()) // create
70 dfolder.create(Folder.HOLDS_MESSAGES);
71
72 Message[] msgs = folder.getMessages(start, end);
73 System.out.println("Got messages...");
74
75 // Copy messages into destination,
76 folder.copyMessages(msgs, dfolder);
77 System.out.println("Copied messages...");
78
79 // Close the folder and store
80 folder.close(false);
81 store.close();
82 System.out.println("Closed folder and store...");
83
84 } catch (Exception e) {
85 e.printStackTrace();
86 }
87
88 System.exit(0);
89 }
90 }