Java interoperability and mailx

136 views
Skip to first unread message

Serge Wroclawski

unread,
May 24, 2009, 8:41:03 AM5/24/09
to Clojure Study Group Washington DC
I'm trying to write the Java interop code for my mail handler in Clojure.

Instead of reinventing RFC2822 parsing, I'm trying to use the Sun mail
libraries.

http://java.sun.com/products/javamail/javadocs/javax/mail/package-summary.html

Here's the code I'm roughly trying to translate into Clojure:

package examples;

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

import org.apache.log4j.*;

public class simpleread {
private static Logger log = Logger.getLogger(simpleread.class);
public static void main(String args[])
throws Exception
{
//set up the Logger
BasicConfigurator.configure();

Session session = Session.getInstance(new Properties());
//url examples
String user = "zhukov"; //well this is my home :) you should enter
your name here
String absolute_url = "maildir:/home/"+user+"/Maildir";
String absolute_url2 = "maildir:////home/"+user+"/Maildir";
String relative_url = "maildir:///testhome/Maildir";
String url = absolute_url;

Store store = session.getStore(new URLName(url));

store.connect(); //useless with Maildir but included here for consistency
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_WRITE);
Message m = inbox.getMessage(1);
m.writeTo(System.out);
System.out.println("subject of this message: " + m.getSubject());
m.writeTo(System.out);
}
}


I don't need any of the logging stuff, of course. All I want is to get
the store, folder and messages.

Here's what I have thusfar:

(def session (javax.mail.Session/getInstance(java.util.Properties.)))
(. session getStore (javax.mail.URLName. "maildir:///home/serge/tmp/2008-09"))

And the error I get:

javax.mail.NoSuchProviderException: No provider for maildir (NO_SOURCE_FILE:0)


Anyone have any ideas about this?

- Serge

Keith Bennett

unread,
May 24, 2009, 12:30:46 PM5/24/09
to clojure-...@googlegroups.com
Serge -

I looked at this problem some and have some rough ideas that will not
solve the problem but may possibly help point you in the right
direction if you haven't already been there.

It looks like Sun's mail jar file is not sufficient -- you need an
implementation of the maildir protocol. So you'll need to get a jar
file containing the maildir implementation, include that jar file in
your classpath, and also, I think, register that implementation in
a .properties file.

According to a blog entry at http://www.raditha.com/blog/archives/000348.html
, the properties file is called javamail.providers, and it needs to be
in the jre/lib subdirectory of your Java home directory.

I looked in Sun's mail jar file (unzip -v mail-1.4.jar | less), and
found a META-INF/javamail.default.providers file. That might be
something that's automatically queried before your javamail.providers
file, or just an example file to be imitated, I don't know.

I've been trying this on my Mac, and the Mac's Java implementation is
written by Apple, not Sun, I believe, so with outlying stuff like
this, there may be differences. (Some of the classes in the com.sun
package tree, as opposed to the java package tree, may not be provided
by default on the Mac.)

Another thing to be aware of is that these days a great deal of Java
programming is done on code that is run within an application server
such as JBoss or Weblogic; these application servers may provide
things in their environment that we will not have by default.
Similarly, some articles, etc., will assume the server environment, so
one needs to mentally translate the text to that of a serverless
implementation.

- Keith

Serge Wroclawski

unread,
May 24, 2009, 5:54:29 PM5/24/09
to clojure-...@googlegroups.com
On Sun, May 24, 2009 at 12:30 PM, Keith Bennett <keithr...@gmail.com> wrote:
>
> Serge -
>
> I looked at this problem some and have some rough ideas that will not
> solve the problem but may possibly help point you in the right
> direction if you haven't already been there.
>
> It looks like Sun's mail jar file is not sufficient -- you need an
> implementation of the maildir protocol.

I think I see my problem. I was using the example from:

http://javamaildir.sourceforge.net/

> So you'll need to get a jar
> file containing the maildir implementation, include that jar file in
> your classpath, and also, I think, register that implementation in
> a .properties file.

How does one register the implementation?

> According to a blog entry at http://www.raditha.com/blog/archives/000348.html
> , the properties file is called javamail.providers, and it needs to be
> in the jre/lib subdirectory of your Java home directory.

Of my actual Java? I can't have it in my home dir?

- Serge

Keith Bennett

unread,
May 24, 2009, 8:18:03 PM5/24/09
to clojure-...@googlegroups.com
Serge -

I'm kind of stabbing in the dark here, as I don't really know this stuff
either, but I'll give it a try...

Serge Wroclawski wrote:
> On Sun, May 24, 2009 at 12:30 PM, Keith Bennett <keithr...@gmail.com> wrote:
>
>> Serge -
>>
>> I looked at this problem some and have some rough ideas that will not
>> solve the problem but may possibly help point you in the right
>> direction if you haven't already been there.
>>
>> It looks like Sun's mail jar file is not sufficient -- you need an
>> implementation of the maildir protocol.
>>
>
> I think I see my problem. I was using the example from:
>
> http://javamaildir.sourceforge.net/
>
>
>> So you'll need to get a jar
>> file containing the maildir implementation, include that jar file in
>> your classpath, and also, I think, register that implementation in
>> a .properties file.
>>
>
> How does one register the implementation?
>
>

Not sure, but I think it needs to be in that javamail.providers file. I
downloaded and reviewed the Java Mail source code, and it looks like the
provider information is indeed read from a resource at runtime, but I
couldn't figure out much more than that. (If anyone's interested, check
out the constructor for the Session class; it calls loadProviders().) I
can't tell where the stream is getting its data from; maybe the Session
class is in some kind of context that takes care of that. In any case,
the javamail.providers file might be a good place to start.

OTOH, if you would be willing to test with POP or IMAP instead of
maildir, then I think you could bypass all of this configuration stuff,
because those are provided by Sun out of the box.


>> According to a blog entry at http://www.raditha.com/blog/archives/000348.html
>> , the properties file is called javamail.providers, and it needs to be
>> in the jre/lib subdirectory of your Java home directory.
>>
>
> Of my actual Java? I can't have it in my home dir?
>

Yes, the jre/lib subdirectory of the root of your Java software
installation.

From the point of view of Java, very little is configured in the home
directory. The only thing I know of that's there is the user
preferences, which is in the hidden ".java" subdirectory of home.

When the Java runtime needs to find something, it's almost always the
classpath that matters. So if it can go anywhere other than jre/lib, it
would probably have to be somewhere in your classpath. I'd suggest
trying jre/lib first, and if you can get that to work, then possibly
moving it somewhere else if you want. (You could always keep a
"personal" copy of the JDK in a subdirectory of your home directory if
you don't want to much with a system-ish installation. I realize that's
a hack, though.)

- Keith

> - Serge
>

Serge Wroclawski

unread,
May 24, 2009, 9:37:07 PM5/24/09
to clojure-...@googlegroups.com
I think I may just scrap this entirely and move on to the ical one...

Thanks for the help... *grumble grumble Java*

- Serge

Keith Bennett

unread,
May 25, 2009, 9:35:48 AM5/25/09
to clojure-...@googlegroups.com
Serge -

I just posted a message to Novajug about it at:

http://tech.groups.yahoo.com/group/novajug/message/13272

Maybe if they give you a helpful answer you won't have to abandon it...

- Keith

---
Keith R. Bennett

Serge Wroclawski

unread,
May 25, 2009, 10:12:18 AM5/25/09
to clojure-...@googlegroups.com
On Mon, May 25, 2009 at 9:35 AM, Keith Bennett <keithr...@gmail.com> wrote:
>
> Serge -
>
> I just posted a message to Novajug about it at:
>
> http://tech.groups.yahoo.com/group/novajug/message/13272
>
> Maybe if they give you a helpful answer you won't have to abandon it...

Well, it was only interesting as a component. I agreed to the ical
component too, and ical4j is failing to build (I think I know why
though, and I need to work through it).

I'm trying to stay focused on fixing the problems, but I guess I'm
just a bit shocked at the difficulty thusfar in getting everything to
work. I realize how spoiled I am with {Perl|Python|Ruby}.

I'll keep at it though.

- Serge

Keith Bennett

unread,
May 25, 2009, 2:13:46 PM5/25/09
to clojure-...@googlegroups.com
Serge -

I don't know if you've seen it yet, but there was a suggestion on
novajug to use courier-imap as an IMAP server to wrap around maildir
directories:

http://tech.groups.yahoo.com/group/novajug/message/13273

I can understand why you'd want to access the maildir's directly, but
there would be advantages to using courier-imap; mainly that you could
avoid all this maildir support hell in your Java code since IMAP is
supported out of the box, but also that the IMAP support in javax.mail
is likely to be more tested and reliable than a third party maildir
product (again, probably but not definitely).

Do Perl, Python, and Ruby all have reliable and easy-to-use maildir
implementations?

- Keith

Serge Wroclawski

unread,
May 25, 2009, 2:43:27 PM5/25/09
to clojure-...@googlegroups.com
On Mon, May 25, 2009 at 2:13 PM, Keith Bennett <keithr...@gmail.com> wrote:
>
> Serge -
>
> I don't know if you've seen it yet, but there was a suggestion on
> novajug to use courier-imap as an IMAP server to wrap around maildir
> directories:

The point of this excercise was to be able to use email from the pipes
project. I was just trying to get myself up to the point of processing
a single mail. Setting up an imap server just for that is too much for
me.

> Do Perl, Python, and Ruby all have reliable and easy-to-use maildir
> implementations?

With the above three, I can just get whatever software I need. Perl
has CPAN. Ruby has Gems, Python eggs exist, but even without them, in
any of the languages, I dunno, stuff just "works". I can usually just
use apt-get or yum.

- Serge

Keith Bennett

unread,
May 25, 2009, 4:58:31 PM5/25/09
to clojure-...@googlegroups.com
Serge -


On May 25, 2009, at 2:43 PM, Serge Wroclawski wrote:

>
> On Mon, May 25, 2009 at 2:13 PM, Keith Bennett <keithr...@gmail.com
> > wrote:
>>
>> Serge -
>>
>> I don't know if you've seen it yet, but there was a suggestion on
>> novajug to use courier-imap as an IMAP server to wrap around maildir
>> directories:
>
> The point of this excercise was to be able to use email from the pipes
> project. I was just trying to get myself up to the point of processing
> a single mail. Setting up an imap server just for that is too much for
> me.
>

I understand. Another approach would be to start a (disposable)
Google Mail account, enable IMAP in the preferences, and then use
that. But I understand that you have had your fill of this...


>> Do Perl, Python, and Ruby all have reliable and easy-to-use maildir
>> implementations?
>
> With the above three, I can just get whatever software I need. Perl
> has CPAN. Ruby has Gems, Python eggs exist, but even without them, in
> any of the languages, I dunno, stuff just "works". I can usually just
> use apt-get or yum.
>

You probably have a lot more experience with those languages than I
do, but I got burned recently by assuming that. I had to do some SOAP
work and used the only (to my knowledge) usable library for it in Ruby
-- SOAP4R. It was quite disappointing...no disrespect meant to the
authors, who contributed their time generously. If I had to do it
again, I'd look into using JRuby and a Java SOAP library. That's not
to say that Ruby has other libraries that are better than Java's; I
guess all languages and environments have areas in which they are
strong and others in which they are not.

- Keith

Reply all
Reply to author
Forward
0 new messages