Hi, I have an application which needs to connect to an imap store. This work fine on some occasions, but if thunderbird or outlook has the imap folder open or has had it open recently, the application cannot connect. I'm using a simple test application which just connects to an imap folder and get the number of messages, to solve this before implementing the solution into the original application. The imap server is courier run on a redhat linux machine. the code is::
package imaptest;
import java.io.*;
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.*;
import javax.mail.internet.MimeMessage;
import com.sun.mail.imap.*;
import com.sun.mail.imap.Quota.Resource;
public class Main {
public static void main(String[] args) {
String storeType = "imap";
String hostName = "flynt";
int portNo = 143;
String username = "353862222222";
String password = "123456";
String inboxFolderName = "INBOX";
try {
Properties sysProperties = System.getProperties();
Session session = Session.getDefaultInstance(sysProperties, null);
session.setDebug(true);
Store store = (Store)session.getStore(storeType);
if (store.isConnected())
System.out.println("Store is already connected");
else
System.out.println("Store is not connected");
//this is where the exception occurs
store.connect(hostName, portNo, username, password);
System.out.println("Connecting to the IMAP server on " + hostName + " at port " + portNo + " with username '" + username + "'");
System.out.println("Opening INBOX folder");
IMAPFolder inboxFolder = (IMAPFolder)store.getFolder(inboxFolderName);
inboxFolder.open(Folder.READ_ONLY);
int noOfMessages = inboxFolder.getMessageCount();
System.out.println("Found " + noOfMessages + " in INBOX");
if (noOfMessages > 0) {
Message[] msgs = inboxFolder.getMessages();
for (int msgNum = 0; msgNum < msgs.length; msgNum++) {
Part messagePart = msgs[msgNum];
String contentType = messagePart.getContentType();
System.out.println("Message " + msgNum + " has content type " + contentType);
}
}
inboxFolder.close(true);
store.close();
} catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
The followong exception is given:
Store is not connected
;
nested exception is:
java.io.IOException
javax.mail.MessagingException: ;
nested exception is:
java.io.IOException
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:461)
at javax.mail.Service.connect(Service.java:236)
at imaptest.Main.main(Main.java:37)
the exception occurs at the line:
store.connect(hostName, portNo, username, password);
I'm relatively new to javamail and imap, but i would presume that there should not be a problem of multiple applications connecting to an imap store from the same computer?