dcsimg
Using imap for reading messages from folders other than Inbox
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Bharath_Krishna
Posted On:   Monday, March 28, 2005 05:27 AM

Hello All, I want to use JavaMail to read messages from Exchange Server. We have decided to imap protocol to read the messages. When we try to read messages from Inbox folder we are able to get the messages. But when we try to read from any other folder we are not able to read it and we are getting the following error. D:javamail-1.3.2demo>java GetMessageExample mail4.webindia.com USERNAME PASSWORD folder...........test Exception in thread "main" javax.mail.FolderNotFoundException: test not fou nd at com.sun.mail.imap.IMAPFolder.checkExists(IMAPFolder.java:281) at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:779) at GetMessageExample.main(GetMessageExample.java:26)    More>>

Hello All,

I want to use JavaMail to read messages from Exchange Server. We have decided to imap protocol to read the messages. When we try to read messages from Inbox folder we are able to get the messages. But when we try to read from any other folder we are not able to read it and we are getting the following error.

D:javamail-1.3.2demo>java GetMessageExample mail4.webindia.com USERNAME PASSWORD
folder...........test
Exception in thread "main" javax.mail.FolderNotFoundException: test not fou
nd
at com.sun.mail.imap.IMAPFolder.checkExists(IMAPFolder.java:281)
at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:779)
at GetMessageExample.main(GetMessageExample.java:26)

Is there any way of reading the messages from any other folder other than Inbox using imap

This is the code which we are using.

___________________________________________________


import java.io.*;

import java.util.Properties;

import javax.mail.*;

import javax.mail.internet.*;


public class GetMessageExample {

public static void main (String args[]) throws Exception {

String host = args[0];

String username = args[1];

String password = args[2];


// Create empty properties

Properties props = new Properties();


// Get session

Session session = Session.getInstance(props, null);


// Get the store

Store store = session.getStore("imap");

store.connect(host, username, password);

System.out.println("store..........."+store);

//System.out.println("store.......store.getDefaultFolder
()...."+store.getDefaultFolder());

// Get folder

Folder folder = store.getFolder("test");

System.out.println("folder..........."+folder);

folder.open(Folder.READ_ONLY);


BufferedReader reader = new BufferedReader (

new InputStreamReader(System.in));


// Get directory

Message message[] = folder.getMessages();

System.out.println("message..........."+message.length);

for (int i=0, n=message.length; i

System.out.println(i + ": " + message[i].getFrom()[0]

+ " " + message[i].getSubject());


System.out.println(

"Do you want to read message? [YES to read/QUIT to
end]");
String line = reader.readLine();

// Mark as deleted if appropriate

if ("YES".equals(line)) {

message[i].writeTo(System.out);

} else if ("QUIT".equals(line)) {

break;

}

}


// Close connection

folder.close(false);

store.close();

}

}


_____________________________________________________



Please let us know about this as soon as possible.

Bharath Krishna

   <<Less

Re: Using imap for reading messages from folders other than Inbox

Posted By:   Christopher_Koenigsberg  
Posted On:   Monday, March 28, 2005 04:42 PM

My guess is that unless someone has the answer already written down here, you will have to use IMAP-specific methods (e.g. cast your "Store" to the Sun "IMAPStore class", if possible).... to experiment and find out about the namespaces you can access, and how to ask for them, among the folders on the server.


The "Store" interface in the JavaMail API has to work on the lowest common denominator, which is POP. POP servers only have the one DefaultFolder available, with no "namespaces", but IMAP servers can offer multiple namespaces for accessing folders owned by the same user, by other users, also shared folders.


A quick look at javadoc e.g. for the Sun IMAPStore class yields interesting possibilities including "getSharedNamespaces()", "getPersonalNamespaces()", "getUserNamespaces", ...


Also for the "getFolder()" method it says, re the "name" parameter, "... name - The name of the Folder. In some Stores, name can be an absolute path if it starts with the hierarchy delimiter. Else it is interpreted relative to the 'root' of this namespace..."
About | Sitemap | Contact