2

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
android-json-org-java-4.3-0.2.r3.1.fc21.noarch
snappy-java-1.0.5-2.fc21.noarch
tzdata-java-2015b-1.fc21.noarch
python-javapackages-4.1.0-7.fc21.noarch
postgresql94-jdbc-javadoc-9.3.1101-1.f21.noarch
protobuf-java-2.5.0-11.fc21.x86_64
java-1.8.0-openjdk-1.8.0.40-25.b25.fc21.x86_64
java-1.8.0-openjdk-headless-1.8.0.40-25.b25.fc21.x86_64
javassist-3.18.1-2.fc21.noarch
apache-commons-javaflow-1.0-0.8.20120509SNAPSHOT.fc21.noarch
javapackages-tools-4.1.0-7.fc21.noarch
java-1.8.0-openjdk-devel-1.8.0.40-25.b25.fc21.x86_64
antlr3-java-3.5.2-2.fc21.noarch
javamail-1.5.1-3.fc21.noarch
xz-java-1.5-3.fc21.noarch
abrt-java-connector-1.1.0-2.fc21.x86_64

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.

Community
  • 1
  • 1
Richard T
  • 4,570
  • 5
  • 37
  • 49

1 Answers1

2

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

EDITED BY QUESTION ASKER:

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!

Richard T
  • 4,570
  • 5
  • 37
  • 49
Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • I had set this problem aside for a while - used ripmime to get most of the job done, now I actually do need to fix this. It turns out that you're mistaken, I have in fact ATTEMPTED to import javax.mail.internet, and, even more specifically, javax.mail.internet.MimeUtility. I'll update the Q. – Richard T Oct 14 '16 at 21:30
  • Above you only have "import javax.mail.*;", did you add "import javax.mail.internet.*"? Importing only MimeUtility doesn't give you access to the entire package. – Bill Shannon Oct 14 '16 at 22:20
  • I get that, Bill, but ANY use of the class would be a major step forward. It won't get past the import statement at present! Unless you're saying I would have success by importing the whole thing?! Odd. But at this point I'll try anything. – Richard T Oct 14 '16 at 23:08
  • DAMN! Changing the import to from "import javax.mail.internet;" (followed on the next line by the MimeUtility import) to "javax.mail.internet.*;" ACTUALLY WORKED! -DAMN- Do I Feel Foolish! ... Would have continued to not see if it you hadn't commented! – Richard T Oct 14 '16 at 23:11
  • Ah ha! Now I can delete the long response I was working on. :-) – Bill Shannon Oct 14 '16 at 23:13
  • Thanks again for the help. Note that very early on in my experience with Java - say, around 1997 - I got into the habit of explicitly loading each and every class I want to use. This way, it's very clear from the import statements what any given bit of code requires and therefore helps with keeping up with changes. as they come along. The few times I've ever imported an entire class like that - without the asterisk - it's worked, and I never knew it was because I just got lucky there was something at that level to import! Lesson learned - I hope! – Richard T Oct 14 '16 at 23:23