List of usage examples for javax.mail Folder close
public abstract void close(boolean expunge) throws MessagingException;
From source file:org.wso2.carbon.registry.es.utils.EmailUtil.java
/** * This method read verification e-mail from Gmail inbox and returns the verification URL. * * @return verification redirection URL. * @throws Exception/*from www . j a va 2 s . co m*/ */ public static String readGmailInboxForVerification() throws Exception { boolean isEmailVerified = false; long waitTime = 10000; String pointBrowserURL = ""; Properties props = new Properties(); props.load(new FileInputStream(new File(TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator + "smtp.properties"))); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString()); Folder inbox = store.getFolder("inbox"); inbox.open(Folder.READ_WRITE); Thread.sleep(waitTime); long startTime = System.currentTimeMillis(); long endTime = 0; int count = 1; while (endTime - startTime < 180000 && !isEmailVerified) { Message[] messages = inbox.getMessages(); for (Message message : messages) { if (!message.isExpunged()) { try { log.info("Mail Subject:- " + message.getSubject()); if (message.getSubject().contains("EmailVerification")) { pointBrowserURL = getBodyFromMessage(message); isEmailVerified = true; } // Optional : deleting the mail message.setFlag(Flags.Flag.DELETED, true); } catch (MessageRemovedException e) { log.error("Could not read the message subject. Message is removed from inbox"); } } } endTime = System.currentTimeMillis(); Thread.sleep(waitTime); endTime += count * waitTime; count++; } inbox.close(true); store.close(); return pointBrowserURL; }
From source file:edu.stanford.muse.email.MboxEmailStore.java
private void collect_mbox_folders(List<FolderInfo> list, File f) throws MessagingException { if (!f.exists()) return;/*from w w w .ja v a 2s. c o m*/ boolean noChildren = !f.isDirectory(); if (noChildren) { FolderInfo cachedFolderInfo = folderCache.lookup(f.getPath()); if (cachedFolderInfo != null) { if (cachedFolderInfo.messageCount > 0) list.add(cachedFolderInfo); log.info(Util.blurPath(f.getPath()) + " is in the folder cache, message count: " + cachedFolderInfo.messageCount); } else { try { // read the file String path = f.getPath(); if (path.endsWith(".msf")) return; // explicitly ignore msf files, sometimes it seems to read an actual number of messages even from msf files folderBeingScanned = f.getPath(); // folderBeingScannedShortName = Util.stripFrom(f.getPath(), rootPath + File.separatorChar); int idx = folderBeingScanned.lastIndexOf(File.separatorChar); if (idx >= 0 && (idx + 1) < folderBeingScanned.length()) folderBeingScannedShortName = folderBeingScanned.substring(idx + 1); else folderBeingScannedShortName = folderBeingScanned; Pair<Folder, Integer> pair = openFolder(null, f.getPath()); int count = pair.getSecond(); if (count == 1) { // many files are wrongly considered mbox with count 1. Ignore them if they also have a suffix that is known to cause noise. we're being cautious and ignoring these files only if they are noisy if (path.endsWith(".plist")) return; if (path.endsWith(".lock")) return; if (path.endsWith(".DS_Store")) // explicitly ignore .DS_store and ._.DS_Store files, annoying Mac OS binary files that get read as mbox by our parser return; log.info("Ignoring file " + path + " because it has only 1 message and its name matches a suffix that indicates it's likely not an mbox file."); } Folder f1 = pair.getFirst(); boolean validFolder = count > 0 && f1 != null; // we'll cache the folder info even if its not a valid folder. // this ensures we don't have to scan invalid folders again // if (validFolder) { if (f1 != null) f1.close(false); // put the info in the cache FolderInfo fi = new FolderInfo(null, folderBeingScanned, folderBeingScannedShortName, count); folderCache.put(f.getPath(), fi); if (validFolder) list.add(fi); folderBeingScanned = null; } } catch (Exception e) { log.error("Exception trying to read file " + f + ": " + e); } // store.close(); } } else { File filesInDir[] = f.listFiles(); if (filesInDir != null) // somehow this can be null when run on /tmp (maybe due to soft links etc). for (File child : filesInDir) collect_mbox_folders(list, child); } }
From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java
/** * Close any {@link Folder} that contain {@link Message} and are in open state. * * @param folder {@link Folder} to be closed *//*from w w w. ja va 2 s.co m*/ private void closeFolder(Folder folder) { if (folder != null) { try { if (folder.isOpen()) { folder.close(true); } else { LOG.info(folder.getName() + " folder is already open."); } } catch (Exception e) { LOG.warn("Cannot close folder : " + folder.getName(), e); } } }
From source file:org.xwiki.contrib.mail.internal.AbstractMailStore.java
/** * {@inheritDoc}//from w w w . ja va 2 s . c o m * * @see org.xwiki.contrib.mail.IMailReader#readFromStore(java.lang.String) */ @Override public List<Message> read(String folder, boolean onlyUnred, int max) throws MessagingException { Store store = getJavamailStore(); store.connect(); Folder mailFolder = store.getDefaultFolder(); if (StringUtils.isNotEmpty(folder)) { mailFolder = mailFolder.getFolder(folder); } mailFolder.open(Folder.READ_WRITE); Message[] msgsArray = mailFolder.getMessages(); if (max > 0 && msgsArray.length > max) { msgsArray = (Message[]) ArrayUtils.subarray(msgsArray, 0, max); } List<Message> messages = new ArrayList<Message>(Arrays.asList(msgsArray)); mailFolder.close(false); store.close(); return messages; }
From source file:com.jaeksoft.searchlib.crawler.mailbox.crawler.MailboxAbstractCrawler.java
protected void checkFolder(Folder folder, PrintWriter pw) throws MessagingException, IOException, SearchLibException { if (folder == null) return;//from w w w .j a v a 2 s .c o m if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) { folder.open(Folder.READ_ONLY); try { pw.print("Folder "); pw.print(folder.getName()); pw.print(": "); pw.print(folder.getMessageCount()); pw.println(" msgs(s)."); } finally { folder.close(false); } } if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) { Folder[] folders = folder.list(); if (folders != null) for (Folder f : folders) checkFolder(f, pw); } }
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.j a v a 2 s.c o m 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:org.syncope.core.notification.NotificationTest.java
private boolean verifyMail(final String sender, final String subject) { LOG.info("Waiting for notification to be sent..."); try {//from w w w.ja va 2 s . c o m Thread.sleep(10000); } catch (InterruptedException e) { } boolean found = false; try { Session session = Session.getDefaultInstance(System.getProperties()); Store store = session.getStore("pop3"); store.connect(pop3Host, mailAddress, mailPassword); Folder inbox = store.getFolder("INBOX"); assertNotNull(inbox); inbox.open(Folder.READ_WRITE); Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { if (sender.equals(messages[i].getFrom()[0].toString()) && subject.equals(messages[i].getSubject())) { found = true; messages[i].setFlag(Flag.DELETED, true); } } inbox.close(true); store.close(); } catch (Throwable t) { LOG.error("Unexpected exception", t); fail("Unexpected exception while fetching e-mail"); } return found; }
From source file:org.apache.axis2.transport.mail.MailClient.java
public int checkInbox(int mode) throws MessagingException, IOException { int numMessages = 0; if (mode == 0) { return 0; }// ww w.ja va 2 s .co m boolean show = (mode & SHOW_MESSAGES) > 0; boolean clear = (mode & CLEAR_MESSAGES) > 0; String action = (show ? "Show" : "") + ((show && clear) ? " and " : "") + (clear ? "Clear" : ""); log.info(action + " INBOX for " + from); Store store = session.getStore(); store.connect(); Folder root = store.getDefaultFolder(); Folder inbox = root.getFolder("inbox"); inbox.open(Folder.READ_WRITE); Message[] msgs = inbox.getMessages(); numMessages = msgs.length; if ((msgs.length == 0) && show) { log.info("No messages in inbox"); } for (int i = 0; i < msgs.length; i++) { MimeMessage msg = (MimeMessage) msgs[i]; if (show) { log.info(" From: " + msg.getFrom()[0]); log.info(" Subject: " + msg.getSubject()); log.info(" Content: " + msg.getContent()); } if (clear) { msg.setFlag(Flags.Flag.DELETED, true); } } inbox.close(true); store.close(); return numMessages; }
From source file:com.treesbearfruit.icloudnotes.NotesSaver.java
private void save(Store store, String wheretobackup, String f) throws MessagingException, IOException { System.out.println("opening folder " + f); Folder folder = store.getFolder(f); folder.open(Folder.READ_ONLY);/*from w w w .j a va 2s.c om*/ FileUtils.forceMkdir(new File(wheretobackup)); // Get directory Message message[] = folder.getMessages(); for (int i = 0, n = message.length; i < n; i++) { // String from = (message[i].getFrom()[0]).toString(); String subj = (message[i].getSubject()).toString(); String nota = (message[i].getContent()).toString(); // System.out.println("from: " + from); System.out.println("saving: " + subj); // BACKUP NOTE generals.writeFile(wheretobackup + "/" + generals.makeFilename(subj).trim() + ".html", nota, message[i].getSentDate()); } folder.close(false); }
From source file:de.saly.elasticsearch.imap.AbstractIMAPRiverUnitTest.java
protected void deleteMailsFromUserMailbox(final Properties props, final String folderName, final int start, final int deleteCount, final String user, final String password) throws MessagingException { final Store store = Session.getInstance(props).getStore(); store.connect(user, password);/* w w w. j av a 2 s . c o m*/ checkStoreForTestConnection(store); final Folder f = store.getFolder(folderName); f.open(Folder.READ_WRITE); final int msgCount = f.getMessageCount(); final Message[] m = deleteCount == -1 ? f.getMessages() : f.getMessages(start, Math.min(msgCount, deleteCount + start - 1)); int d = 0; for (final Message message : m) { message.setFlag(Flag.DELETED, true); logger.info("Delete msgnum: {} with sid {}", message.getMessageNumber(), message.getSubject()); d++; } f.close(true); logger.info("Deleted " + d + " messages"); store.close(); }