Discussion:
[Classpathx-javamail] Re: Maildir is not visible
Matej Cepl
2007-05-22 13:03:12 UTC
Permalink
OK, this is probably another newbie question, but let me try. I have
this testing program to copy all messages from one mbox folder to
So, with help from people on comp.lang.java.help I found a solution which
at least compiles and works somehow. However, not correctly. This is the
source:

import java.util.*;
import javax.mail.*;

public class CopyFolder {

/**
* @param args
* @throws MessagingException
*/
public static void main(String[] args) throws \
MessagingException {
String urlStr = "/home/matej/.eclipse/workspace"+
"/kmail2tbfolders/examples/";
Session session = Session.getInstance(new \
Properties());
session.setDebug(true);
URLName storeURL = new URLName("mbox://"+urlStr); Store mboxStore
= session.getStore(storeURL); mboxStore.connect();
Folder mboxFolder = mboxStore.getDefaultFolder(); mboxFolder =
mboxStore.getFolder("test"); mboxFolder.open(Folder.READ_ONLY);

URLName store2URL = new URLName("maildir://"+
urlStr+"/output/");
Store maildirStore = session.getStore(store2URL); Folder
targetFolder = maildirStore.getDefaultFolder(); targetFolder =
targetFolder.getFolder("target"); if (!targetFolder.exists()) {
try {
targetFolder.create (Folder.HOLDS_MESSAGES);
}
catch(Exception e) {
System.err.println("Cannot create target foldr: "+
e.getLocalizedMessage());
throw new RuntimeException(e);
}
}
System.out.println("name of the folder is "+
targetFolder.getName());
targetFolder.open(Folder.READ_WRITE);

Message[] messages = mboxFolder.getMessages();
System.out.println("There are "+mboxFolder.getMessageCount()+
" messages in the source folder");
System.out.println("There are "+messages.length+
" messages in the selection");
targetFolder.appendMessages(messages); System.out.println("There
are "+targetFolder.getMessageCount()+
" messages in the target folder");

mboxStore.close();
maildirStore.close();
}

}

However, when I run it I don't get expected results:

[***@hubmaier kmail2tbfolders]$ rm -rf examples/output/ [***@hubmaier
kmail2tbfolders]$ ls -lh examples/ celkem 2,0M
-rw-rw-rw- 1 matej matej 2,0M kvÄ› 2 20:23 test [***@hubmaier
kmail2tbfolders]$ java CopyFolder name of the folder is target
There are 40 messages in the source folder There are 40 messages in the
selection There are 0 messages in the target folder [***@hubmaier
kmail2tbfolders]$ find examples/output/target/* -type f | wc -l
6
[***@hubmaier kmail2tbfolders]$

And truly there are only 6 messages in the create maildir folder. Any
ideas why this doesn't work as expected? I feel so close, and yet I am
still not there!

Thanks for any reply,

Matej
Chris Burdess
2007-05-23 09:30:22 UTC
Permalink
Post by Matej Cepl
So, with help from people on comp.lang.java.help I found a solution which
at least compiles and works somehow. However, not correctly. This is the
import java.util.*;
import javax.mail.*;
public class CopyFolder {
/**
*/
public static void main(String[] args) throws \
MessagingException {
String urlStr = "/home/matej/.eclipse/workspace"+
"/kmail2tbfolders/examples/";
Session session = Session.getInstance(new \
Properties());
session.setDebug(true);
URLName storeURL = new URLName("mbox://"+urlStr); Store mboxStore
= session.getStore(storeURL); mboxStore.connect();
Folder mboxFolder = mboxStore.getDefaultFolder();
mboxFolder =
mboxStore.getFolder("test"); mboxFolder.open
(Folder.READ_ONLY);
URLName store2URL = new URLName("maildir://"+
urlStr+"/output/");
Store maildirStore = session.getStore(store2URL); Folder
targetFolder = maildirStore.getDefaultFolder();
targetFolder =
targetFolder.getFolder("target"); if (!targetFolder.exists
()) {
try {
targetFolder.create (Folder.HOLDS_MESSAGES);
}
catch(Exception e) {
System.err.println("Cannot create target foldr: "+
e.getLocalizedMessage());
throw new RuntimeException(e);
}
}
System.out.println("name of the folder is "+
targetFolder.getName());
targetFolder.open(Folder.READ_WRITE);
Message[] messages = mboxFolder.getMessages();
System.out.println("There are "+mboxFolder.getMessageCount()+
" messages in the source folder");
System.out.println("There are "+messages.length+
" messages in the selection");
targetFolder.appendMessages(messages); System.out.println
("There
are "+targetFolder.getMessageCount()+
" messages in the target folder");
mboxStore.close();
maildirStore.close();
}
}
kmail2tbfolders]$ ls -lh examples/ celkem 2,0M
kmail2tbfolders]$ java CopyFolder name of the folder is target
There are 40 messages in the source folder There are 40 messages in the
kmail2tbfolders]$ find examples/output/target/* -type f | wc -l
6
And truly there are only 6 messages in the create maildir folder. Any
ideas why this doesn't work as expected? I feel so close, and yet I am
still not there!
Thanks for any reply,
There shouldn't be any actual files in the maildir folder. There
should just be 3 directories: cur, new, and tmp. The messages should
have been created in the tmp dir, and then renamed to its final
location in either cur or new, depending on whether they contain the
SEEN flag. What patform are you using, perhaps it doesn't implement
File.renameTo correctly? We could add some validation after the
renameTo to ensure that the target file was created properly in each
instance, and bail with an appropriate error message if not.

You should also register a MessageCountListener on the target folder
to see which messages were actually added. For instance, only
MimeMessages can be appended to a MaildirFolder.
Matej Cepl
2007-05-23 13:13:46 UTC
Permalink
Post by Chris Burdess
Post by Matej Cepl
$ java CopyFolder
name of the folder is target
There are 40 messages in the source folder
There are 40 messages in the selection
There are 0 messages in the target folder
This is what I have problem with -- why there are no messages in the
target folder even according to my Java program?
Post by Chris Burdess
Post by Matej Cepl
$ find examples/output/target/* -type f | wc -l
6
$
There shouldn't be any actual files in the maildir folder.
find <dirname> -type f lists all files (not directories) in all
subdirectories of <dirname>. So that's not the problem. Of course, I
checked this with mutt as well (I used find only because it is is easier
to show in the message).
Post by Chris Burdess
What patform are you using, perhaps it doesn't implement File.renameTo
correctly?
Linux -- Fedora Core 6
Post by Chris Burdess
You should also register a MessageCountListener on the target folder to
see which messages were actually added. For instance, only MimeMessages
can be appended to a MaildirFolder.
That's interesting. I will get back to you later when I will check this.

Thanks,

Matej

Loading...