encrypted javamail MimeMultipart

E

eunever32

Hi

I am trying to obtain the attachment from an encrypted email.

The content when I decrypt looks like below.

How can I cast this to a MimeMultipart ?

The content after I decrypt is a ByteArrayInputStream
decryptedContent = envelopedBodyPart.getUnencryptedContent( cert,
privateKey );

(I am using com.isnetworks EnvelopedBodyPart.getUnencrypted.)

The message is decrypting correctly as below but I am not getting the
MimeMultipart that would allow me to get the attachment correctly.
Thanks in advance.

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="file100.txt"
Content-Disposition: attachment; filename="file100.txt"
Content-Transfer-Encoding: quoted-printable

etc
 
E

eunever32

Hi

I am trying to obtain the attachment from an encrypted email.

The content when I decrypt looks like below.

How can I cast this to a MimeMultipart ?

The content after I decrypt is a ByteArrayInputStream
decryptedContent = envelopedBodyPart.getUnencryptedContent( cert,
privateKey );

(I am using com.isnetworks EnvelopedBodyPart.getUnencrypted.)

The message is decrypting correctly as below but I am not getting the
MimeMultipart that would allow me to get the attachment correctly.
Thanks in advance.

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="file100.txt"
Content-Disposition: attachment; filename="file100.txt"
Content-Transfer-Encoding: quoted-printable

etc

By the way

This is how the mailcap is defined:

mailcap.addMailcap( "application/x-pkcs7-mime; ;" +
"x-java-content-
handler=com.isnetworks.smime.activation." +
"EnvelopedBodyPartDataContentHandler" );

mailcap.addMailcap( "application/pkcs7-mime; ;" +
"x-java-content-handler=com.isnetworks.smime.activation."
+
"EnvelopedBodyPartDataContentHandler" );

mailcap.addMailcap( "multipart/signed; ;" +
"x-java-content-handler=com.isnetworks.smime.activation."
+
"SignedMultipartDataContentHandler" );

mailcap.addMailcap( "application/x-pkcs7-signature; ; " +
"x-java-content-handler=com.isnetworks.smime.activation."
+
"SignedBodyPartDataContentHandler" );

mailcap.addMailcap( "application/pkcs7-signature; ; " +
"x-java-content-handler=com.isnetworks.smime.activation."
+
"SignedBodyPartDataContentHandler" );

Do I need something extra here? Such as multipart/alternative ??

I tried this but not sure exactly how to code for this
 
R

Roedy Green

How can I cast this to a MimeMultipart ?

I have not done this myself, but it seems to me you need four steps:

1. get the armoured encrypted message characters, with headers
stripped off.

2. strip off the armouring so you have an array of bytes.

3. run this through the decryption algorithm.

4. convert the decrypted bytes back into characters via some encoding.
 
M

Martin Gregorie

I have not done this myself, but it seems to me you need four steps:

1. get the armoured encrypted message characters, with headers stripped
off.

2. strip off the armouring so you have an array of bytes.

3. run this through the decryption algorithm.

4. convert the decrypted bytes back into characters via some encoding.

(4) should become:
- create a ByteArrayInputStream from the decrypted byte array
- pass that to a MimeMessage constructor.

At this point you can use the standard MimeMessage and MultiPart
methods to parse the message and extract its content.
 
M

Martin Gregorie

I have not done this myself, but it seems to me you need four steps:

1. get the armoured encrypted message characters, with headers stripped
off.

2. strip off the armouring so you have an array of bytes.

3. run this through the decryption algorithm.

4. convert the decrypted bytes back into characters via some encoding.

(4) should become:
- create a ByteArrayInputStream from the decrypted byte array
- pass that to a MimeMessage constructor.

At this point you can use the standard MimeMessage and MultiPart
methods to parse the message and extract its content.
 
M

Martin Gregorie

I have not done this myself, but it seems to me you need four steps:

1. get the armoured encrypted message characters, with headers stripped
off.

2. strip off the armouring so you have an array of bytes.

3. run this through the decryption algorithm.

4. convert the decrypted bytes back into characters via some encoding.

(4) should become:
- create a ByteArrayInputStream from the decrypted byte array
- pass that to a MimeMessage constructor.

At this point you can use the standard MimeMessage and MultiPart
methods to parse the message and extract its content.
 
M

Martin Gregorie

I have not done this myself, but it seems to me you need four steps:

1. get the armoured encrypted message characters, with headers stripped
off.

2. strip off the armouring so you have an array of bytes.

3. run this through the decryption algorithm.

4. convert the decrypted bytes back into characters via some encoding.

(4) should become:
- create a ByteArrayInputStream from the decrypted byte array
- pass that to a MimeMessage constructor.

At this point you can use the standard MimeMessage and MultiPart
methods to parse the message and extract its content.
 
M

Martin Gregorie

I have not done this myself, but it seems to me you need four steps:

1. get the armoured encrypted message characters, with headers stripped
off.

2. strip off the armouring so you have an array of bytes.

3. run this through the decryption algorithm.

4. convert the decrypted bytes back into characters via some encoding.

(4) should become:
- create a ByteArrayInputStream from the decrypted byte array
- pass that to a MimeMessage constructor.

At this point you can use the standard MimeMessage and MultiPart
methods to parse the message and extract its content.
 
M

Martin Gregorie

I have not done this myself, but it seems to me you need four steps:

1. get the armoured encrypted message characters, with headers stripped
off.

2. strip off the armouring so you have an array of bytes.

3. run this through the decryption algorithm.

4. convert the decrypted bytes back into characters via some encoding.

(4) should become:
- create a ByteArrayInputStream from the decrypted byte array
- pass that to a MimeMessage constructor.

At this point you can use the standard MimeMessage and MultiPart
methods to parse the message and extract its content.
 
E

eunever32

(4) should become:
    - create a ByteArrayInputStream from the decrypted byte array
    - pass that to a MimeMessage constructor.

    At this point you can use the standard MimeMessage and MultiPart
    methods to parse the message and extract its content.

--
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |- Hide quoted text -

- Show quoted text -


Thanks Martin but maybe you can help me:

As I said I'm new to this Javamail api and am looking for a succinct
way to obtain the attachment

I can do

MimeMessage msg = new MimeMessage(session, new
FileInputStream("file.txt"));

The resulting msg has three headers which looks right

But when I do
if (msg.getContent() instanceof Multipart) {
saveAttachment
}

It transpires it's NOT an object of type Multipart ... so what can I
do ???

I think at this stage it's basic javamail I require but am struggling.

Thanks.
 
E

eunever32

Thanks Martin but maybe you can help me:

As I said I'm new to this Javamail api and am looking for a succinct
way to obtain the attachment

I can do

MimeMessage msg = new MimeMessage(session, new
FileInputStream("file.txt"));

The resulting msg has three headers which looks right

But when I do
if (msg.getContent() instanceof Multipart) {
     saveAttachment

}

It transpires it's NOT an object of type Multipart ... so what can I
do ???

I think at this stage it's basic javamail I require but am struggling.

Thanks.- Hide quoted text -

- Show quoted text -

In other words when I try this
Multipart multipart = (Multipart) content;
Exception in thread "main" java.lang.ClassCastException:
com.sun.mail.util.SharedByteArrayInputStream
 
M

Martin Gregorie

Sorry for all the repeats - the NNTP server I use threw a wobbly this
morning and I hadn't realised it was accepting the post *and then*
locking up until just now.
True enough: MimeMessage.getContent() can return a lot of things
including InputStreams - thats why it returns an Object!

Did you do what I suggested and download both the JavaMail Design
Specification and the API Documentation? If not, go get them now and read
them. The first example in Design Specification Appendix B shows exactly
how to parse a multipart MIME message.

The Appendix B examples are all available as downloadable source code, so
you can run them and/or swipe useful code from them.
 
L

Lothar Kimmeringer

Roedy said:
I have not done this myself, but it seems to me you need four steps:

1. get the armoured encrypted message characters, with headers
stripped off.
2. strip off the armouring so you have an array of bytes.
3. run this through the decryption algorithm.
4. convert the decrypted bytes back into characters via some encoding.

Why do you follow the NIH-principle and not suggest using one
of the SMIME-utils out there? "eunever32" is using one already
and he has already decrypted the message as you'd know if you
actually read the original post instead of scanning for keywords.

Implementing SMIME-handling for yourself is a very bad idea. I
just got through a certification of a connector I implemented
where SMIME is used for encryption and signing of data and most
problems that where arising there (not on my side BTW) where wrong
implementations of hash-calculations etc. Just an example. An
8bit-encoded attachment must be normalized before the digest is
calculated. Many don't know this ending up with wrong hash-values
preventing the data-transfer to succeed.


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

Sorry for all the repeats - the NNTP server I use threw a wobbly this
morning and I hadn't realised it was accepting the post *and then*
locking up until just now.






True enough: MimeMessage.getContent() can return a lot of things
including InputStreams - thats why it returns an Object!

Did you do what I suggested and download both the JavaMail Design
Specification and the API Documentation? If not, go get them now and read
them. The first example in Design Specification Appendix B shows exactly
how to parse a multipart MIME message.

The Appendix B examples are all available as downloadable source code, so
you can run them and/or swipe useful code from them.

--
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |- Hide quoted text -

- Show quoted text -

Martin

Thanks for your suggestion and I have obtained the Javamail Design
document you describe.
It is very good and I now know about Message, Part, MimePart,
MimeMultipart, MimeMessage and I can see there is an example of how to
read attachments.
I am not at my desk right now so I can't verify it however I am
concerned that if I do the following:

Object content = decryptContent(message, key, publicKey);
And then I try

if (content instanceof MimePart) {
...
}
if (content instanceof MimeMultiPart) {
...
}
if (content instanceof InputStream) {
...
}

And if my code finds itself in "InputStream" then I am back to square
one (?)
And how then do I obtain the attachment which I clearly have

Thanks again to everyone for their suggestions.
 
M

Martin Gregorie

Martin

Thanks for your suggestion and I have obtained the Javamail Design
document you describe.
It is very good and I now know about Message, Part, MimePart,
MimeMultipart, MimeMessage and I can see there is an example of how to
read attachments.
I am not at my desk right now so I can't verify it however I am
concerned that if I do the following:

Object content = decryptContent(message, key, publicKey); And then I try

if (content instanceof MimePart) {
...
}
if (content instanceof MimeMultiPart) {
...
}
if (content instanceof InputStream) {
...
}

And if my code finds itself in "InputStream" then I am back to square
one (?)
And how then do I obtain the attachment which I clearly have
That depends what you want to do with it - you can read the InputStream
into an array, write it to a file,.... whatever your application requires.
You should, of course, be looking at the Part's headers to see what
you've got (and hence how you need to handle it) begoe doing anything
with the content. And don't forget that a MIME message is a recursive
structure. It may simply have a String as its root node (it its a simple
plain text message), but OTOH it might contain a MIME message which
contains one or more MIME messages as their attachments, which in turn....
 
E

eunever32

That depends what you want to do with it - you can read the InputStream
into an array, write it to a file,.... whatever your application requires..
You should, of course, be looking at the Part's headers to see what
you've got (and hence how you need to handle it) begoe doing anything
with the content. And don't forget that a MIME message is a recursive
structure. It may simply have a String as its root node (it its a simple
plain text message), but OTOH it might contain a MIME message which
contains one or more MIME messages as their attachments, which in turn.....

--
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |- Hide quoted text -

- Show quoted text -

For anyone else who encounters this problem... I want to let you
know .. the problem was due to using an old version of Javamail

When I upgraded to Javamail 1.4.3 it worked.
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top