List of usage examples for javax.mail Store close
public synchronized void close() throws MessagingException
From source file:Main.java
public static void main(String[] args) throws Exception { Properties props = new Properties(); String host = "yourserver.edu"; String provider = "pop3"; Session session = Session.getDefaultInstance(props, new MailAuthenticator()); Store store = session.getStore(provider); store.connect(host, null, null);/* w w w . ja v a 2 s . c o m*/ Folder inbox = store.getFolder("INBOX"); if (inbox == null) { System.out.println("No INBOX"); System.exit(1); } inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { System.out.println("Message " + (i + 1)); messages[i].writeTo(System.out); } inbox.close(false); store.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Properties props = new Properties(); String host = "yourHost.edu"; String username = "userName"; String password = "mypassword"; String provider = "pop3"; Session session = Session.getDefaultInstance(props, null); Store store = session.getStore(provider); store.connect(host, username, password); Folder inbox = store.getFolder("INBOX"); if (inbox == null) { System.out.println("No INBOX"); System.exit(1);//from w ww . ja va2 s. com } inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { System.out.println("Message " + (i + 1)); messages[i].writeTo(System.out); } inbox.close(false); store.close(); }
From source file:copier.java
public static void main(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;// w ww . j av a 2s .co m } 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); }
From source file:MainClass.java
public static void main(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;// w ww . j a v a 2s.c o m } 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); }
From source file:GetMessageExample.java
public static void main(String args[]) throws Exception { if (args.length != 3) { System.err.println("Usage: java MailExample host username password"); System.exit(-1);/*from ww w .j av a2 s.c om*/ } String host = args[0]; String username = args[1]; String password = args[2]; // Create empty properties Properties props = new Properties(); // Get session Session session = Session.getDefaultInstance(props, null); // Get the store Store store = session.getStore("pop3"); store.connect(host, username, password); // Get folder Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); // Get directory Message message[] = folder.getMessages(); for (int i = 0, n = message.length; i < n; i++) { System.out.println(i + ": " + message[i].getFrom()[0] + "\t" + message[i].getSubject()); System.out.println("Read message? [YES to read/QUIT to end]"); String line = reader.readLine(); if ("YES".equalsIgnoreCase(line)) { System.out.println(message[i].getContent()); } else if ("QUIT".equalsIgnoreCase(line)) { break; } } // Close connection folder.close(false); store.close(); }
From source file:namespace.java
public static void main(String argv[]) { int msgnum = -1; int optind;//from w ww . j a va 2 s . co m for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { host = argv[++optind]; } else if (argv[optind].equals("-U")) { user = argv[++optind]; } else if (argv[optind].equals("-P")) { password = argv[++optind]; } else if (argv[optind].equals("-D")) { debug = true; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-p")) { port = Integer.parseInt(argv[++optind]); } else if (argv[optind].equals("-u")) { suser = argv[++optind]; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: namespace [-L url] [-T protocol] [-H host] [-p port] [-U user]"); System.out.println("\t[-P password] [-u other-user] [-D]"); System.exit(1); } else { break; } } try { // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); session.setDebug(debug); // Get a Store object Store store = null; if (url != null) { URLName urln = new URLName(url); store = session.getStore(urln); store.connect(); } else { if (protocol != null) store = session.getStore(protocol); else store = session.getStore(); // Connect if (host != null || user != null || password != null) store.connect(host, port, user, password); else store.connect(); } printFolders("Personal", store.getPersonalNamespaces()); printFolders("User \"" + suser + "\"", store.getUserNamespaces(suser)); printFolders("Shared", store.getSharedNamespaces()); store.close(); } catch (Exception ex) { System.out.println("Oops, got exception! " + ex.getMessage()); ex.printStackTrace(); } System.exit(0); }
From source file:folderlist.java
public static void main(String argv[]) throws Exception { int optind;// w w w . j a va 2 s . co m for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { host = argv[++optind]; } else if (argv[optind].equals("-U")) { user = argv[++optind]; } else if (argv[optind].equals("-P")) { password = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-R")) { root = argv[++optind]; } else if (argv[optind].equals("-r")) { recursive = true; } else if (argv[optind].equals("-v")) { verbose = true; } else if (argv[optind].equals("-D")) { debug = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: folderlist [-T protocol] [-H host] [-U user] [-P password] [-L url]"); System.out.println("\t[-R root] [-r] [-v] [-D] [pattern]"); System.exit(1); } else { break; } } if (optind < argv.length) pattern = argv[optind]; // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); session.setDebug(debug); // Get a Store object Store store = null; Folder rf = null; if (url != null) { URLName urln = new URLName(url); store = session.getStore(urln); store.connect(); } else { if (protocol != null) store = session.getStore(protocol); else store = session.getStore(); // Connect if (host != null || user != null || password != null) store.connect(host, user, password); else store.connect(); } // List namespace if (root != null) rf = store.getFolder(root); else rf = store.getDefaultFolder(); dumpFolder(rf, false, ""); if ((rf.getType() & Folder.HOLDS_FOLDERS) != 0) { Folder[] f = rf.list(pattern); for (int i = 0; i < f.length; i++) dumpFolder(f[i], recursive, " "); } store.close(); }
From source file:mover.java
public static void main(String argv[]) { int start = 1; int end = -1; int optind;//from w w w. ja va 2 s . c o m for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { // protocol protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { // host host = argv[++optind]; } else if (argv[optind].equals("-U")) { // user user = argv[++optind]; } else if (argv[optind].equals("-P")) { // password password = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-s")) { // Source mbox src = argv[++optind]; } else if (argv[optind].equals("-d")) { // Destination mbox dest = argv[++optind]; } else if (argv[optind].equals("-x")) { // Expunge ? expunge = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]"); System.out.println("\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]"); System.out.println("\t The -x option => EXPUNGE deleted messages"); System.out.println("\t msgnum1 => start of message-range; msgnum2 => end of message-range"); System.exit(1); } else { break; } } if (optind < argv.length) start = Integer.parseInt(argv[optind++]); // start msg if (optind < argv.length) end = Integer.parseInt(argv[optind++]); // end msg try { // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); // Get a Store object Store store = null; if (url != null) { URLName urln = new URLName(url); store = session.getStore(urln); store.connect(); } else { if (protocol != null) store = session.getStore(protocol); else store = session.getStore(); // Connect if (host != null || user != null || password != null) store.connect(host, user, password); else store.connect(); } // Open source Folder Folder folder = store.getFolder(src); if (folder == null || !folder.exists()) { System.out.println("Invalid folder: " + src); System.exit(1); } folder.open(Folder.READ_WRITE); int count = folder.getMessageCount(); if (count == 0) { // No messages in the source folder System.out.println(folder.getName() + " is empty"); // Close folder, store and return folder.close(false); store.close(); return; } // Open destination folder, create if reqd Folder dfolder = store.getFolder(dest); if (!dfolder.exists()) dfolder.create(Folder.HOLDS_MESSAGES); if (end == -1) end = count; // Get the message objects to copy Message[] msgs = folder.getMessages(start, end); System.out.println("Moving " + msgs.length + " messages"); if (msgs.length != 0) { folder.copyMessages(msgs, dfolder); folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true); // Dump out the Flags of the moved messages, to insure that // all got deleted for (int i = 0; i < msgs.length; i++) { if (!msgs[i].isSet(Flags.Flag.DELETED)) System.out.println("Message # " + msgs[i] + " not deleted"); } } // Close folders and store folder.close(expunge); store.close(); } catch (MessagingException mex) { Exception ex = mex; do { System.out.println(ex.getMessage()); if (ex instanceof MessagingException) ex = ((MessagingException) ex).getNextException(); else ex = null; } while (ex != null); } }
From source file:MainClass.java
public static void main(String argv[]) { int start = 1; int end = -1; int optind;// www .j av a 2 s . c o m for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { // protocol protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { // host host = argv[++optind]; } else if (argv[optind].equals("-U")) { // user user = argv[++optind]; } else if (argv[optind].equals("-P")) { // password password = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-s")) { // Source mbox src = argv[++optind]; } else if (argv[optind].equals("-d")) { // Destination mbox dest = argv[++optind]; } else if (argv[optind].equals("-x")) { // Expunge ? expunge = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]"); System.out.println("\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]"); System.out.println("\t The -x option => EXPUNGE deleted messages"); System.out.println("\t msgnum1 => start of message-range; msgnum2 => end of message-range"); System.exit(1); } else { break; } } if (optind < argv.length) start = Integer.parseInt(argv[optind++]); // start msg if (optind < argv.length) end = Integer.parseInt(argv[optind++]); // end msg try { // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); // Get a Store object Store store = null; if (url != null) { URLName urln = new URLName(url); store = session.getStore(urln); store.connect(); } else { if (protocol != null) store = session.getStore(protocol); else store = session.getStore(); // Connect if (host != null || user != null || password != null) store.connect(host, user, password); else store.connect(); } // Open source Folder Folder folder = store.getFolder(src); if (folder == null || !folder.exists()) { System.out.println("Invalid folder: " + folder.getName()); System.exit(1); } folder.open(Folder.READ_WRITE); int count = folder.getMessageCount(); if (count == 0) { // No messages in the source folder System.out.println(folder.getName() + " is empty"); // Close folder, store and return folder.close(false); store.close(); return; } // Open destination folder, create if reqd Folder dfolder = store.getFolder(dest); if (!dfolder.exists()) dfolder.create(Folder.HOLDS_MESSAGES); if (end == -1) end = count; // Get the message objects to copy Message[] msgs = folder.getMessages(start, end); System.out.println("Moving " + msgs.length + " messages"); if (msgs.length != 0) { folder.copyMessages(msgs, dfolder); folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true); // Dump out the Flags of the moved messages, to insure that // all got deleted for (int i = 0; i < msgs.length; i++) { if (!msgs[i].isSet(Flags.Flag.DELETED)) System.out.println("Message # " + msgs[i] + " not deleted"); } } // Close folders and store folder.close(expunge); store.close(); } catch (MessagingException mex) { Exception ex = mex; do { System.out.println(ex.getMessage()); if (ex instanceof MessagingException) ex = ((MessagingException) ex).getNextException(); else ex = null; } while (ex != null); } }
From source file:net.prhin.mailman.MailMan.java
public static void main(String[] args) { Properties props = new Properties(); props.setProperty(resourceBundle.getString("mailman.mail.store"), resourceBundle.getString("mailman.protocol")); Session session = Session.getInstance(props); try {/* w w w.j a va2 s. c om*/ Store store = session.getStore(); store.connect(resourceBundle.getString("mailman.host"), resourceBundle.getString("mailman.user"), resourceBundle.getString("mailman.password")); Folder inbox = store.getFolder(resourceBundle.getString("mailman.folder")); inbox.open(Folder.READ_ONLY); inbox.getUnreadMessageCount(); Message[] messages = inbox.getMessages(); for (int i = 0; i <= messages.length / 2; i++) { Message tmpMessage = messages[i]; Multipart multipart = (Multipart) tmpMessage.getContent(); System.out.println("Multipart count: " + multipart.getCount()); for (int j = 0; j < multipart.getCount(); j++) { BodyPart bodyPart = multipart.getBodyPart(j); if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) { if (bodyPart.getContent().getClass().equals(MimeMultipart.class)) { MimeMultipart mimeMultipart = (MimeMultipart) bodyPart.getContent(); for (int k = 0; k < mimeMultipart.getCount(); k++) { if (mimeMultipart.getBodyPart(k).getFileName() != null) { printFileContents(mimeMultipart.getBodyPart(k)); } } } } else { printFileContents(bodyPart); } } } inbox.close(false); store.close(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }