1 38 39 import java.io.*; 40 import java.util.*; 41 import javax.mail.*; 42 import javax.mail.internet.*; 43 44 45 46 public class mover { 47 48 static String protocol = "imap"; 49 static String host = null; 50 static String user = null; 51 static String password = null; 52 static String src = null; 53 static String dest = null; 54 static boolean expunge = false; 55 static String url = null; 56 57 public static void main(String argv[]) { 58 int start = 1; int end = -1; 59 int optind; 60 61 for (optind = 0; optind < argv.length; optind++) { 62 if (argv[optind].equals("-T")) { protocol = argv[++optind]; 64 } else if (argv[optind].equals("-H")) { host = argv[++optind]; 66 } else if (argv[optind].equals("-U")) { user = argv[++optind]; 68 } else if (argv[optind].equals("-P")) { password = argv[++optind]; 70 } else if (argv[optind].equals("-L")) { 71 url = argv[++optind]; 72 } else if (argv[optind].equals("-s")) { src = argv[++optind]; 74 } else if (argv[optind].equals("-d")) { dest = argv[++optind]; 76 } else if (argv[optind].equals("-x")) { expunge = true; 78 } else if (argv[optind].equals("--")) { 79 optind++; 80 break; 81 } else if (argv[optind].startsWith("-")) { 82 System.out.println( 83 "Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]"); 84 System.out.println( 85 "\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]"); 86 System.out.println( 87 "\t The -x option => EXPUNGE deleted messages"); 88 System.out.println( 89 "\t msgnum1 => start of message-range; msgnum2 => end of message-range"); 90 System.exit(1); 91 } else { 92 break; 93 } 94 } 95 96 if (optind < argv.length) 97 start = Integer.parseInt(argv[optind++]); 99 if (optind < argv.length) 100 end = Integer.parseInt(argv[optind++]); 102 try { 103 Properties props = System.getProperties(); 105 106 Session session = Session.getInstance(props, null); 108 109 Store store = null; 111 if (url != null) { 112 URLName urln = new URLName(url); 113 store = session.getStore(urln); 114 store.connect(); 115 } else { 116 if (protocol != null) 117 store = session.getStore(protocol); 118 else 119 store = session.getStore(); 120 121 if (host != null || user != null || password != null) 123 store.connect(host, user, password); 124 else 125 store.connect(); 126 } 127 128 129 Folder folder = store.getFolder(src); 131 if (folder == null || !folder.exists()) { 132 System.out.println("Invalid folder: " + folder.getName()); 133 System.exit(1); 134 } 135 136 folder.open(Folder.READ_WRITE); 137 138 int count = folder.getMessageCount(); 139 if (count == 0) { System.out.println(folder.getName() + " is empty"); 141 folder.close(false); 143 store.close(); 144 return; 145 } 146 147 Folder dfolder = store.getFolder(dest); 149 if (!dfolder.exists()) 150 dfolder.create(Folder.HOLDS_MESSAGES); 151 152 if (end == -1) 153 end = count; 154 155 Message[] msgs = folder.getMessages(start, end); 157 System.out.println("Moving " + msgs.length + " messages"); 158 159 if (msgs.length != 0) { 160 folder.copyMessages(msgs, dfolder); 161 folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true); 162 163 for (int i = 0; i < msgs.length; i++) { 166 if (!msgs[i].isSet(Flags.Flag.DELETED)) 167 System.out.println("Message # " + msgs[i] + 168 " not deleted"); 169 } 170 } 171 172 folder.close(expunge); 174 store.close(); 175 176 } catch (MessagingException mex) { 177 Exception ex = mex; 178 do { 179 System.out.println(ex.getMessage()); 180 if (ex instanceof MessagingException) 181 ex = ((MessagingException)ex).getNextException(); 182 else 183 ex = null; 184 } while (ex != null); 185 } 186 } 187 } 188
| Popular Tags
|