/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2014 Emmanuel Keller / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of OpenSearchServer.
 *
 * OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.crawler.mailbox.crawler;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;

import com.jaeksoft.searchlib.Logging;
import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.crawler.mailbox.MailboxFieldEnum;
import com.jaeksoft.searchlib.crawler.mailbox.MailboxProtocolEnum;
import com.jaeksoft.searchlib.index.IndexDocument;
import com.sun.mail.imap.IMAPMessage;
import com.sun.mail.imap.IMAPStore;

public class IMAP4Crawler extends MailboxAbstractCrawler {

	@Override
	public void read() throws MessagingException, IOException,
			SearchLibException {
		Properties properties = new Properties();
		properties.setProperty("mail.host", item.getServerName());
		properties.setProperty("mail.port",
				Integer.toString(item.getServerPort()));
		String storeProtocol = protocol == MailboxProtocolEnum.IMAP4 ? "imap"
				: "imaps";
		properties.setProperty("mail.transport.protocol", storeProtocol);
		Session session = Session.getInstance(properties,
				new javax.mail.Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(item.getUser(), item
								.getPassword());
					}
				});
		IMAPStore store = (IMAPStore) session.getStore(storeProtocol);
		try {
			store.connect();
			logFolder("PERSONAL NAMESPACES: ", store.getPersonalNamespaces());
			logFolder("SHARED NAMESPACES: ", store.getSharedNamespaces());
			Folder defaultFolder = store.getDefaultFolder();
			Logging.info("DEFAULT FOLDER: " + defaultFolder.getFullName());
			readFolder(defaultFolder);
		} finally {
			if (store != null)
				store.close();
		}
	}

	private void logFolder(String source, Folder[] folders) {
		Logging.info("SOURCE FOLDERS: " + source);
		for (Folder folder : folders)
			Logging.info(folder.getFullName());
	}

	@Override
	public void readMessage(IndexDocument document, Folder folder,
			Message message) throws MessagingException {
		super.readMessage(document, folder, message);
		if (message instanceof IMAPMessage)
			document.addString(MailboxFieldEnum.message_id.name(),
					((IMAPMessage) message).getMessageID());
	}
}

@@ -1135,47 +1135,8 @@
 n;%0A%0A
-import com.jaeksoft.searchlib.Logging;%0A
 impo
@@ -2309,400 +2309,202 @@
 %0A%09%09%09
-logFolder(%22PERSONAL NAMESPACES: %22, store.getPersonalNamespaces());%0A%09%09%09logFolder(%22SHARED NAMESPACES: %22, store.getSharedNamespaces());%0A%09%09%09Folder defaultFolder = store.getDefaultFolder();%0A%09%09%09Logging.info(%22DEFAULT FOLDER: %22 + defaultFolder.getFullName());%0A%09%09%09readFolder(defaultFolder);%0A%09%09%7D finally %7B%0A%09%09%09if (store != null)%0A%09%09%09%09store.close();%0A%09%09%7D%0A%09%7D%0A%0A%09private void logFolder(String source, F
+Folder defaultFolder = store.getDefaultFolder();%0A%09%09%09if (defaultFolder == null)%0A%09%09%09%09return;%0A%09%09%09Folder%5B%5D folders = defaultFolder.list();%0A%09%09%09if (folders == null)%0A%09%09%09%09return;%0A%09%09%09for (Folder f
 older
-%5B%5D
+ :
  fol
@@ -2512,127 +2512,92 @@
 ers)
- %7B
 %0A%09%09
-Logging.info(%22SOURCE FOLDERS: %22 + source);%0A%09%09for (
+%09%09read
 Folder
- 
+(
 folder
- : folders)%0A%09%09%09Logging.info(folder.getFullNam
+);%0A%09%09%7D finally %7B%0A%09%09%09if (store != null)%0A%09%09%09%09store.clos
 e()
-)
 ;%0A
+%09%09%7D%0A
 %09%7D%0A%0A

Fix imap4 crawl issue