Question

I am trying to move from Javamail to mime4j in the hope of having less trouble parsing mail. However I haven't managed to find out how to connect to a mail server to then use mime4j to parse messages.

Javamail provides session and connections to a mail server via IMAP or POP3 from which you then read Messages. As far as I can tell mime4j does not offer anything equivalent.

So how can I connect to mail servers to use mime4j? Can I use Javamail in conjunction with mime4j?

I have tried using the javamail Message.getInputStream method to get an input stream that I pass to mime4j's DefaultMessageBuilder.parseMessage. However the result is not what I expect: for a multipart message containing a plain text part and an html part, only one TextBody is created. I suspect this is because the DefaultMessageBuilder.parseMessage is expecting a stream for the whole message, not just the body, but that javamail Message.getInputStream only provides the body.

In fact, I'm only interested in the body, since this is what was causing problems with Javamail, but parsing the whole message (if I could get it as an input stream form Javamail) would do just as well.

Any help greatly appreciated!

Was it helpful?

Solution

If you're using POP3, all the message parsing happens on the client side in JavaMail. Have you looked at the many properties you can set in JavaMail to configure how parsing errors are handled?

If you're using IMAP, most of the parsing is done by the server. What sort of parsing errors are you still having with JavaMail? Are they problems parsing the content of the email messages, or problems parsing the IMAP protocol messages?

With more details of the problems you're running into, I might be able to help you configure JavaMail to work for you.

If you really want to use another MIME message parser, you'll need to use the Message.writeTo method to get the entire MIME content of a message, which you can pass to some other library to parse.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top