1 6 7 import java.util.*; 8 import java.text.*; 9 import javax.mail.*; 10 11 17 18 public class foldersplit { 19 20 static String protocol; 21 static String host = null; 22 static String user = null; 23 static String password = null; 24 static String dst = null; 25 static String url = null; 26 static int port = -1; 27 static boolean verbose = false; 28 static boolean debug = false; 29 static boolean nop = false; 30 static SimpleDateFormat df = new SimpleDateFormat("yyyy.MM"); 31 32 public static void main(String argv[]) { 33 int optind; 34 35 for (optind = 0; optind < argv.length; optind++) { 36 if (argv[optind].equals("-T")) { 37 protocol = argv[++optind]; 38 } else if (argv[optind].equals("-H")) { 39 host = argv[++optind]; 40 } else if (argv[optind].equals("-U")) { 41 user = argv[++optind]; 42 } else if (argv[optind].equals("-P")) { 43 password = argv[++optind]; 44 } else if (argv[optind].equals("-d")) { 45 dst = argv[++optind]; 46 } else if (argv[optind].equals("-v")) { 47 verbose = true; 48 } else if (argv[optind].equals("-n")) { 49 nop = true; 50 } else if (argv[optind].equals("-D")) { 51 debug = true; 52 } else if (argv[optind].equals("-L")) { 53 url = argv[++optind]; 54 } else if (argv[optind].equals("-p")) { 55 port = Integer.parseInt(argv[++optind]); 56 } else if (argv[optind].equals("--")) { 57 optind++; 58 break; 59 } else if (argv[optind].startsWith("-")) { 60 System.out.println( 61 "Usage: foldersplit [-L url] [-T protocol] [-H host] [-p port] [-U user]"); 62 System.out.println( 63 "\t[-P password] [-v] [-D] folder ..."); 64 System.exit(1); 65 } else { 66 break; 67 } 68 } 69 70 try { 71 Properties props = System.getProperties(); 73 74 Session session = Session.getDefaultInstance(props, null); 76 session.setDebug(debug); 77 78 Store store = null; 80 if (url != null) { 81 URLName urln = new URLName(url); 82 store = session.getStore(urln); 83 store.connect(); 84 } else { 85 if (protocol != null) 86 store = session.getStore(protocol); 87 else 88 store = session.getStore(); 89 90 if (host != null || user != null || password != null) 92 store.connect(host, port, user, password); 93 else 94 store.connect(); 95 } 96 97 98 100 Folder deffolder = store.getDefaultFolder(); 101 if (deffolder == null) { 102 System.out.println("No default folder"); 103 System.exit(1); 104 } 105 Folder dstfolder; 106 if (dst == null) { 107 dstfolder = deffolder; 108 } else { 109 dstfolder = deffolder.getFolder(dst); 110 if (dstfolder == null) { 111 System.out.println("No destination folder"); 112 System.exit(1); 113 } 114 } 115 116 Folder outf = null; 117 for (; optind < argv.length; optind++) { 118 Folder folder = deffolder.getFolder(argv[optind]); 119 if (folder == null) { 120 System.out.println("Invalid folder: " + argv[optind]); 121 continue; 122 } 123 System.out.println("Folder: " + folder.getFullName()); 124 125 folder.open(Folder.READ_ONLY); 126 int totalMessages = folder.getMessageCount(); 127 128 if (totalMessages == 0) { 129 System.out.println("Empty folder"); 130 folder.close(false); 131 continue; 132 } 133 134 if (verbose) { 135 int newMessages = folder.getNewMessageCount(); 136 System.out.println("Total messages = " + totalMessages); 137 System.out.println("New messages = " + newMessages); 138 System.out.println("-------------------------------"); 139 } 140 141 Message[] msgs = folder.getMessages(); 143 144 FetchProfile fp = new FetchProfile(); 146 fp.add(FetchProfile.Item.ENVELOPE); 147 fp.add(FetchProfile.Item.FLAGS); 148 folder.fetch(msgs, fp); 149 150 for (int i = 0; i < msgs.length; i++) { 151 if (verbose) { 152 System.out.println("--------------------------"); 153 System.out.println("MESSAGE #" + (i + 1) + ":"); 154 } 155 Date d = msgs[i].getReceivedDate(); 156 if (d != null) { 157 String n = df.format(d); 158 if (verbose) 159 System.out.println(n); 160 if (outf == null || !n.equals(outf.getName())) { 161 outf = dstfolder.getFolder(n); 162 if (!outf.exists()) { 163 System.out.println("Creating: " + 164 outf.getFullName()); 165 if (!nop) 166 outf.create(Folder.HOLDS_MESSAGES); 167 } 168 } 169 } 170 if (!nop) 171 outf.appendMessages(new Message[] { msgs[i] }); 172 } 173 folder.close(false); 174 } 175 176 store.close(); 177 } catch (Exception ex) { 178 System.out.println("Oops, got exception! " + ex.getMessage()); 179 ex.printStackTrace(); 180 } 181 System.exit(1); 182 } 183 } 184 | Popular Tags |