javamail content not multipart

E

eunever32

Hopefully this clarifies my earlier post.

I have little knowledge of Javamail but I need to read an attachment
from an email

I have the result of a decrypted email stored in a file as below

I can do:

MimeMessage msg = new MimeMessage(session, new
FileInputStream("file.txt"));
The resulting message has three headers which seems correct

msg.getContent() is not an instanceof Multipart

Is there some way to obtain the attachment??

This is a multipart message in MIME format.
--=_mixed 00561CF28025770E_=
Content-Type: multipart/alternative; boundary="=_alternative
00561CF28025770E_="


--=_alternative 00561CF28025770E_=
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: base64


--=_alternative 00561CF28025770E_=
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: base64

DQo=
--=_alternative 00561CF28025770E_=--
--=_mixed 00561CF28025770E_=
Content-Type: text/plain; name="TEST.txt"
Content-Disposition: attachment; filename="TEST.txt"
Content-Transfer-Encoding: quoted-printable

Some text

Some more text

And some more
--=_mixed 00561CF28025770E_=--
 
L

Lothar Kimmeringer

I have little knowledge of Javamail but I need to read an attachment
from an email

I have the result of a decrypted email stored in a file as below

I can do:

MimeMessage msg = new MimeMessage(session, new
FileInputStream("file.txt"));
The resulting message has three headers which seems correct

Three headers for a mail is too less. From, To, Subject and
Date are only the standard ones and we're not talking about
the headers you need to get a MIME-message
msg.getContent() is not an instanceof Multipart

What is the type of getContent, what is the complete decrypted
mail (i.e. with the headers). Without that nobody will be able
to tell you more than: Something doesn't seem to be right.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
E

eunever32

Three headers for a mail is too less. From, To, Subject and
Date are only the standard ones and we're not talking about
the headers you need to get a MIME-message


What is the type of getContent, what is the complete decrypted
mail (i.e. with the headers). Without that nobody will be able
to tell you more than: Something doesn't seem to be right.

Regards, Lothar
--
Lothar Kimmeringer                E-Mail: (e-mail address removed)
               PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                 questions!

Lothar

Thanks for the reply.

The type of the content is ByteArrayInputStream

The full text of the email is as follows (I'm not storing the from, to
etc.)
Thanks
STARTS HERE
This is a multipart message in MIME format.
--=_mixed 00561CF28025770E_=
Content-Type: multipart/alternative; boundary="=_alternative
00561CF28025770E_="


--=_alternative 00561CF28025770E_=
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: base64


--=_alternative 00561CF28025770E_=
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: base64

DQo=
--=_alternative 00561CF28025770E_=--
--=_mixed 00561CF28025770E_=
Content-Type: text/plain; name="TEST.txt"
Content-Disposition: attachment; filename="TEST.txt"
Content-Transfer-Encoding: quoted-printable

Some text

Some more text

And some more
--=_mixed 00561CF28025770E_=--
ENDS HERE
 
L

Lew

Don't quote sigs.
The type of the content is ByteArrayInputStream

You did an illegal thing in Java.

Java is strongly typed. Types are a set of compiler-enforced constraints.
You declared something like (and you should share an SSCCE):

InputStream content = new ByteArrayInputStream( contentBytes );

For the variable type I purposely substituted the general 'InputStream' for
the specific 'ByteArrayInputStream' you undoubtedly used.

You have a 'msg.getContent()' that returns that 'content'. I'll give it
credit for a declaration 'public InputStream getContent()'.

But there's absolutely no way whatsoever that Java will give any kind of
'InputStream' credit for being a subtype or supertype of 'Multipart'. That's
just your fundamental type-strong compiler constraints following the rules of
the language, there.

There must be some action, not a declarative type cast, that you really want
to perform with that 'getContent()'. What is it?

Oh, and you should share an SSCCE.
http://sscce.org/
 
M

Martin Gregorie

You have a 'msg.getContent()' that returns that 'content'. I'll give it
credit for a declaration 'public InputStream getContent()'.
Actually, it returns an Object.

Disassembling that is a recursive process, which you start by getting the
body as a Part with MimeMessage.getContent(). Then you can inspect the
Part's headers to determine what it is with MimeType and decide what
you're going to do with it, such as write it to a file before using
getContent() to get its content as an Object and then using the
instanceof operator to determine what you got, which is normally one of:

- String. Used for plain text - the headers give the encoding.

- InputStream. This is used for binary and Base64 attachments.

- MultiPart. Dissembling the message body is a recursive process:
an attachment might be a MIME message with its own attachments.
There must be some action, not a declarative type cast, that you really
want to perform with that 'getContent()'. What is it?
If the OP had done what I said in the first place and read the JavaMail
Design Specification he would already know this *and* would be in
possession of example code that does exactly what I've just described.
 
L

Lothar Kimmeringer

The full text of the email is as follows (I'm not storing the from, to
etc.)

If you throw away the mail-headers, the parser is not able to
recognize that the mail is a multipart message. So no wonder,
you don't get wrong results.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
E

eunever32

If you throw away the mail-headers, the parser is not able to
recognize that the mail is a multipart message. So no wonder,
you don't get wrong results.

Regards, Lothar
--
Lothar Kimmeringer                E-Mail: (e-mail address removed)
               PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                 questions!

For anyone else who encounters this issue I thought I would share the
solution:

I upgraded my version of Javamail to 1.4.3 ( I had been using 1.3)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top