Richard T Richard T - 1 year ago 143
Java Question

javax.mail.MimeMessage not available even though package is in CLASSPATH

I'm trying to develop an email parser - to take email that's in a file and be able to programmatically handle the various components - to know who the sender and recipient were, the subject line, main body, and any attachments. I intend to extract attachments as individual files, but I'm stumped right at the beginning.

I started with an already working java program in a fully-functional development environment and have begun adding to it. One of the first additions was this line (then later, set of lines):

import javax.mail.*;
import javax.mail.internet;
import javax.mail.internet.MimeUtility;

Later, in an appropriate place, we have this humble beginning:

MimeMessage m = null;

I was shocked to find that the compile failed with only these two changes, and I learned here (on another StackOverflow page) that the package javax.mail package isn't included in the standard JDK. Puzzled, I looked and found this:

# rpm -qa | grep -i java

Please note that yes, actually, JavaMail is installed - version 1.5.1-3. However, just to be a belts and suspenders kinda person, I found the JavaMail project and downloaded the latest production version, and put it in the CLASSPATH.

To my great surprise, this did not cure the problem! Being the careful type, and keeping things simple to prove the way as I go, I simply removed the javax.mail.jar file from the library directory, then removed the two lines from my program, recompiled and it worked. Then, I added back in the import line, and it failed. Then I moved the jar file back into the library directory and the compile succeeded, confirming that the javax.mail package was being loaded.

However, when I added in the first reference to the library, MimeMessage (see the line above), the compile failed.

So, of course, I went to check the documentation! Indeed, MimeMessage is an available class.

What am I doing wrong?! I mean, geez, I've been using Java literally since version 1.0 - I'm not known for making too many dumb mistakes, but I figure I must be!

Two open questions come to my mind:

1) Why isn't the installed version of the JavaMail package being used? CLEARLY I had to add it. And do note that I don't have to do ANYTHING special to CLASSPATH (or anywhere else) to get all the rest of Java! And;

2) Now that I've got a JavaMail package in there, why is it not discovering the MimeMessage class?

Any / all help appreciated - and if you spot me being abjectly stupid, please point out my error gently!

Additional Information:
I added two more import lines, so there are now the three listed above. Curiously, when I have the .jar file in the path specified by CLASSPATH, but not explicitly cited, I get one error per import, but when I explicitly cite the .jar file as an explicit item, I only get ONE error! That one error is on:

import javax.mail.internet;

Naturally, knowing that the jar is actually just a zip, I unzipped and looked. Sure enough, a directory named "internet" is there, populated with 38 class files.

Somewhere along the way, I had a wild idea that somehow there was ANOTHER javax.mail entry on the system or in the CLASSPATH, so I looked. This is Fedora Core, so it's under /usr/lib, and I did NOT find another file including the string "mail" in its name, so I presume that's not it, either.

I noticed in the NOTES.txt file that there were some notes about not unpacking the jar for some applications because that was seen as a security risk, but I figured that for what I want to do there is no such risk and maybe it would work, but I couldn't get THAT to work either...

I've also been trying using -cp versus actually altering the CLASSPATH variable, but it doesn't seem to matter. I apparently get identical results either way, so I've just been using -cp for testing because it's faster to try various alternatives.

Answer Source

MimeMessage is in the javax.mail.internet package, which you haven't imported.


Actually, I was trying to import, among other things:

import javax.mail.internet;

And that didn't work. What I was missing was that there's nothing to import on just javax.mail.internet. Changing it to:

import javax.mail.internet.*;

worked just fine!

So, while Bill wasn't quite correct, this answer plus his comment got me to see what I was blind to... and I feel silly!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download