List of usage examples for javax.mail Folder getFullName
public abstract String getFullName();
From source file:populate.java
public static void main(String argv[]) { String srcURL = null;/*from w w w . j av a2 s . c o m*/ String dstURL = null; boolean debug = false; int optind; for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-s")) { srcURL = argv[++optind]; } else if (argv[optind].equals("-d")) { dstURL = argv[++optind]; } else if (argv[optind].equals("-D")) { debug = true; } else if (argv[optind].equals("-f")) { force = true; } else if (argv[optind].equals("-S")) { skipSpecial = true; } else if (argv[optind].equals("-c")) { clear = true; } else if (argv[optind].equals("-P")) { dontPreserveFlags = true; } else if (argv[optind].equals("-W")) { warn = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { printUsage(); System.exit(1); } else { break; } } try { if (srcURL == null || dstURL == null) { printUsage(); System.exit(1); } Session session = Session.getInstance(System.getProperties(), null); session.setDebug(debug); // Get source folder URLName srcURLName = new URLName(srcURL); Folder srcFolder; // Check if the source URL has a folder specified. If // not, we use the default folder if (srcURLName.getFile() == null) { Store s = session.getStore(srcURLName); s.connect(); srcFolder = s.getDefaultFolder(); } else { srcFolder = session.getFolder(new URLName(srcURL)); if (!srcFolder.exists()) { System.out.println("source folder does not exist"); srcFolder.getStore().close(); System.exit(1); } } // Set up destination folder URLName dstURLName = new URLName(dstURL); Folder dstFolder; // Check if the destination URL has a folder specified. If // not, we use the source folder name if (dstURLName.getFile() == null) { Store s = session.getStore(dstURLName); s.connect(); dstFolder = s.getFolder(srcFolder.getName()); } else dstFolder = session.getFolder(dstURLName); if (clear && dstFolder.exists()) { if (!dstFolder.delete(true)) { System.out.println("couldn't delete " + dstFolder.getFullName()); return; } } copy(srcFolder, dstFolder); // Close the respective stores. srcFolder.getStore().close(); dstFolder.getStore().close(); } catch (MessagingException mex) { System.out.println(mex.getMessage()); mex.printStackTrace(); } }
From source file:populate.java
public static void main(String argv[]) { String srcURL = null;/*from w ww.j av a2s .co m*/ String dstURL = null; boolean debug = false; int optind; for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-s")) { srcURL = argv[++optind]; } else if (argv[optind].equals("-d")) { dstURL = argv[++optind]; } else if (argv[optind].equals("-D")) { debug = true; } else if (argv[optind].equals("-f")) { force = true; } else if (argv[optind].equals("-S")) { skipSpecial = true; } else if (argv[optind].equals("-c")) { clear = true; } else if (argv[optind].equals("-P")) { dontPreserveFlags = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { printUsage(); System.exit(1); } else { break; } } try { if (srcURL == null || dstURL == null) { printUsage(); System.exit(1); } Session session = Session.getInstance(System.getProperties(), null); session.setDebug(debug); // Get source folder URLName srcURLName = new URLName(srcURL); Folder srcFolder; // Check if the source URL has a folder specified. If // not, we use the default folder if (srcURLName.getFile() == null) { Store s = session.getStore(srcURLName); s.connect(); srcFolder = s.getDefaultFolder(); } else { srcFolder = session.getFolder(new URLName(srcURL)); if (!srcFolder.exists()) { System.out.println("source folder does not exist"); srcFolder.getStore().close(); System.exit(1); } } // Set up destination folder URLName dstURLName = new URLName(dstURL); Folder dstFolder; // Check if the destination URL has a folder specified. If // not, we use the source folder name if (dstURLName.getFile() == null) { Store s = session.getStore(dstURLName); s.connect(); dstFolder = s.getFolder(srcFolder.getName()); } else dstFolder = session.getFolder(dstURLName); if (clear && dstFolder.exists()) { if (!dstFolder.delete(true)) { System.out.println("couldn't delete " + dstFolder.getFullName()); return; } } copy(srcFolder, dstFolder); // Close the respective stores. srcFolder.getStore().close(); dstFolder.getStore().close(); } catch (MessagingException mex) { System.out.println(mex.getMessage()); mex.printStackTrace(); } }
From source file:populate.java
private static void copy(Folder src, Folder dst) throws MessagingException { System.out.println("Populating " + dst.getFullName()); Folder ddst = dst;//from www .j av a2 s .com Folder[] srcFolders = null; if ((src.getType() & Folder.HOLDS_FOLDERS) != 0) srcFolders = src.list(); boolean srcHasChildren = srcFolders != null && srcFolders.length > 0; if (!dst.exists()) { // Create it. boolean dstHoldsOnlyFolders = false; if (!dst.create(src.getType())) { // might not be able to create a folder that holds both if (!dst.create(srcHasChildren ? Folder.HOLDS_FOLDERS : Folder.HOLDS_MESSAGES)) { // might only be able to create one type (Gmail) if (!dst.create(Folder.HOLDS_MESSAGES)) { System.out.println("couldn't create " + dst.getFullName()); return; } } dstHoldsOnlyFolders = srcHasChildren; } // Copy over any messges from src to dst if ((src.getType() & Folder.HOLDS_MESSAGES) != 0) { src.open(Folder.READ_ONLY); if (dstHoldsOnlyFolders) { if (src.getMessageCount() > 0) { System.out.println("Unable to copy messages from " + src.getFullName() + " to " + dst.getFullName() + " because destination holds only folders"); } } else copyMessages(src, dst); src.close(false); } } else { System.out.println(dst.getFullName() + " already exists"); // Copy over any messges from src to dst if (force && (src.getType() & Folder.HOLDS_MESSAGES) != 0) { src.open(Folder.READ_ONLY); copyMessages(src, dst); src.close(false); } } // Copy over subfolders if (srcHasChildren) { for (int i = 0; i < srcFolders.length; i++) { // skip special directories? if (skipSpecial) { if (srcFolders[i].getName().equals("SCCS") || srcFolders[i].getName().equals("Drafts") || srcFolders[i].getName().equals("Trash") || srcFolders[i].getName().equals("Shared Folders")) continue; } copy(srcFolders[i], dst.getFolder(srcFolders[i].getName())); } } }
From source file:populate.java
private static void copy(Folder src, Folder dst) throws MessagingException { System.out.println("Populating " + dst.getFullName()); Folder ddst = dst;//from w w w. ja va 2 s . c om Folder[] srcFolders = null; if ((src.getType() & Folder.HOLDS_FOLDERS) != 0) srcFolders = src.list(); boolean srcHasChildren = srcFolders != null && srcFolders.length > 0; if (!dst.exists()) { // Create it. boolean dstHoldsOnlyFolders = false; try { if (!dst.create(src.getType())) { System.out.println("couldn't create " + dst.getFullName()); return; } } catch (MessagingException mex) { // might not be able to create a folder that holds both if (src.getType() != (Folder.HOLDS_MESSAGES | Folder.HOLDS_FOLDERS)) throw mex; if (!dst.create(srcHasChildren ? Folder.HOLDS_FOLDERS : Folder.HOLDS_MESSAGES)) { System.out.println("couldn't create " + dst.getFullName()); return; } dstHoldsOnlyFolders = srcHasChildren; } // Copy over any messges from src to dst if ((src.getType() & Folder.HOLDS_MESSAGES) != 0) { src.open(Folder.READ_ONLY); if (dstHoldsOnlyFolders) { if (src.getMessageCount() > 0) { System.out.println("Unable to copy messages from " + src.getFullName() + " to " + dst.getFullName() + " because destination holds only folders"); } } else copyMessages(src, dst); src.close(false); } } else { System.out.println(dst.getFullName() + " already exists"); // Copy over any messges from src to dst if (force && (src.getType() & Folder.HOLDS_MESSAGES) != 0) { src.open(Folder.READ_ONLY); copyMessages(src, dst); src.close(false); } } // Copy over subfolders if (srcHasChildren) { for (int i = 0; i < srcFolders.length; i++) { // skip special directories? if (skipSpecial) { if (srcFolders[i].getName().equals("SCCS") || srcFolders[i].getName().equals("Drafts") || srcFolders[i].getName().equals("Trash") || srcFolders[i].getName().equals("Shared Folders")) continue; } copy(srcFolders[i], dst.getFolder(srcFolders[i].getName())); } } }
From source file:folderlist.java
static void dumpFolder(Folder folder, boolean recurse, String tab) throws Exception { System.out.println(tab + "Name: " + folder.getName()); System.out.println(tab + "Full Name: " + folder.getFullName()); System.out.println(tab + "URL: " + folder.getURLName()); if (verbose) { if (!folder.isSubscribed()) System.out.println(tab + "Not Subscribed"); if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) { if (folder.hasNewMessages()) System.out.println(tab + "Has New Messages"); System.out.println(tab + "Total Messages: " + folder.getMessageCount()); System.out.println(tab + "New Messages: " + folder.getNewMessageCount()); System.out.println(tab + "Unread Messages: " + folder.getUnreadMessageCount()); }/*from w w w . j a v a2s . c o m*/ if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) System.out.println(tab + "Is Directory"); /* * Demonstrate use of IMAP folder attributes * returned by the IMAP LIST response. */ if (folder instanceof IMAPFolder) { IMAPFolder f = (IMAPFolder) folder; String[] attrs = f.getAttributes(); if (attrs != null && attrs.length > 0) { System.out.println(tab + "IMAP Attributes:"); for (int i = 0; i < attrs.length; i++) System.out.println(tab + " " + attrs[i]); } } } System.out.println(); if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) { if (recurse) { Folder[] f = folder.list(); for (int i = 0; i < f.length; i++) dumpFolder(f[i], recurse, tab + " "); } } }
From source file:MainClass.java
static void dumpFolder(Folder folder, boolean recurse, String tab) throws Exception { System.out.println(tab + "Name: " + folder.getName()); System.out.println(tab + "Full Name: " + folder.getFullName()); System.out.println(tab + "URL: " + folder.getURLName()); if (verbose) { if (!folder.isSubscribed()) System.out.println(tab + "Not Subscribed"); if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) { if (folder.hasNewMessages()) System.out.println(tab + "Has New Messages"); System.out.println(tab + "Total Messages: " + folder.getMessageCount()); System.out.println(tab + "New Messages: " + folder.getNewMessageCount()); System.out.println(tab + "Unread Messages: " + folder.getUnreadMessageCount()); }//from w ww . j a v a 2s. co m if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) System.out.println(tab + "Is Directory"); /* * Demonstrate use of IMAP folder attributes returned by the IMAP LIST * response. */ if (folder instanceof IMAPFolder) { IMAPFolder f = (IMAPFolder) folder; String[] attrs = f.getAttributes(); if (attrs != null && attrs.length > 0) { System.out.println(tab + "IMAP Attributes:"); for (int i = 0; i < attrs.length; i++) System.out.println(tab + " " + attrs[i]); } } } System.out.println(); if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) { if (recurse) { Folder[] f = folder.list(); for (int i = 0; i < f.length; i++) dumpFolder(f[i], recurse, tab + " "); } } }
From source file:edu.hawaii.soest.hioos.storx.StorXEmailUtil.java
/** * Move messages from the parent backlog folder to year-month folders based * on the date sent of the message/*from w w w.j av a 2 s. c o m*/ * * @param targetMailbox * @param sourceMailbox */ public static void organizeMessages(String sourceMailbox, String targetMailbox) { ArrayList<String> years = getYears(); ArrayList<String> months = getMonths(); try { targetParentFolder = mailStore.getFolder(targetMailbox); targetParentFolder.open(Folder.READ_WRITE); log.debug("Target folder: " + targetParentFolder.getName()); sourceParentFolder = mailStore.getFolder(sourceMailbox); sourceParentFolder.open(Folder.READ_WRITE); log.debug("Source folder: " + sourceParentFolder.getName()); Message messages[]; while (!targetParentFolder.isOpen()) { targetParentFolder.open(Folder.READ_WRITE); } messages = (Message[]) sourceParentFolder.getMessages(); log.debug("Processing " + messages.length + " messages."); int count = 0; for (Message message : messages) { Date sentDate = message.getSentDate(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); String folderByDate = formatter.format(sentDate); Folder destinationFolder = targetParentFolder.getFolder(folderByDate); targetParentFolder.copyMessages(new Message[] { message }, destinationFolder); //message.setFlag(Flags.Flag.DELETED, true); count++; log.debug(count + ")" + message.getSentDate() + " moved to\t" + destinationFolder.getFullName()); } } catch (MessagingException e) { e.printStackTrace(); } finally { try { mailStore.close(); } catch (MessagingException e) { e.printStackTrace(); } } }
From source file:populate.java
/** * Copy message from src to dst. If dontPreserveFlags is set * we first copy the messages to memory, clear all the flags, * and then copy to the destination./*from ww w . jav a2 s . c o m*/ */ private static void copyMessages(Folder src, Folder dst) throws MessagingException { Message[] msgs = src.getMessages(); if (dontPreserveFlags) { for (int i = 0; i < msgs.length; i++) { MimeMessage m = new MimeMessage((MimeMessage) msgs[i]); m.setFlags(m.getFlags(), false); msgs[i] = m; } } if (warn) { // have to copy messages one at a time for (int i = 0; i < msgs.length; i++) { try { src.copyMessages(new Message[] { msgs[i] }, dst); } catch (MessagingException mex) { System.out.println("WARNING: Copy of message " + (i + 1) + " from " + src.getFullName() + " to " + dst.getFullName() + " failed: " + mex.toString()); } } } else src.copyMessages(msgs, dst); }
From source file:edu.stanford.muse.email.ImapPopEmailStore.java
/** recursively collect all folder names under f into list */ private void collect_folder_names(Store store, List<FolderInfo> list, Folder f) throws MessagingException { // ignore hidden files if (f.getFullName().startsWith(".")) return;//from w w w. jav a 2 s.c om if (f.getFullName().indexOf("/.") >= 0 || f.getFullName().indexOf("\\.") >= 0) return; // hack for csl-mail which takes too long to return all the folders if (connectOptions.server.startsWith("csl-mail") && f.getFullName().indexOf("/") >= 0) return; // TOFIX: apparently imap folders can have both messages and children Folder f_children[] = null; boolean hasMessages = true, hasChildren = false; boolean isPop = "pop3".equals(connectOptions.protocol) || "pop3s".equals(connectOptions.protocol); if (!isPop) { // if its imap, check for children hasChildren = (f.getType() & Folder.HOLDS_FOLDERS) != 0; hasMessages = (f.getType() & Folder.HOLDS_MESSAGES) != 0; } if (hasMessages) { folderBeingScanned = f.getFullName(); Pair<Folder, Integer> pair = openFolder(store, f.getFullName()); int count = pair.getSecond(); if (count != -1) pair.getFirst().close(false); // System.out.println ("full name = " + Util.blur(f.getFullName()) + " count = " + count); list.add(new FolderInfo(getAccountID(), f.getFullName(), f.getFullName(), count)); folderBeingScanned = null; } if (hasChildren) { f_children = f.list(); for (Folder child : f_children) collect_folder_names(store, list, child); } }
From source file:com.jaeksoft.searchlib.crawler.mailbox.crawler.MailboxAbstractCrawler.java
private void readMessagesFolder(Folder folder) throws MessagingException, IOException, SearchLibException { folder.open(Folder.READ_ONLY);/*from w ww .j a v a 2 s .c o m*/ String folderFullName = folder.getFullName(); try { int max = folder.getMessageCount(); int i = 0; final int buffer = item.getBufferSize(); while (i < max && !thread.isAborted()) { thread.setStatusInfo(CrawlStatus.CRAWL); int end = i + buffer; if (end > max) end = max; Message[] messages = folder.getMessages(i + 1, end); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); folder.fetch(messages, fp); for (Message message : messages) { if (thread.isAborted()) break; i++; String messageId = getMessageId(folder, message); if (StringUtils.isEmpty(messageId)) continue; if (thread.isAlreadyIndexed(messageId)) { thread.incIgnored(); continue; } IndexDocument crawlIndexDocument = new IndexDocument(item.getLang()); IndexDocument parserIndexDocument = new IndexDocument(item.getLang()); crawlIndexDocument.addString(MailboxFieldEnum.folder.name(), folderFullName); try { readMessage(crawlIndexDocument, parserIndexDocument, folder, message, messageId); thread.addDocument(crawlIndexDocument, parserIndexDocument); } catch (Exception e) { Logging.warn(e); thread.incError(); } } } } finally { folder.close(false); } }