From: eunever32 on 11 Jun 2010 09:53 On Jun 11, 2:40 pm, "euneve...(a)yahoo.co.uk" <euneve...(a)yahoo.co.uk> wrote: > On Jun 11, 1:06 pm, Martin Gregorie <mar...(a)address-in-sig.invalid> > wrote: > > > > > > > On Thu, 10 Jun 2010 19:04:36 -0700, Roedy Green wrote: > > > On Thu, 10 Jun 2010 02:04:40 -0700 (PDT), "euneve...(a)yahoo.co.uk" > > > <euneve...(a)yahoo.co.uk> wrote, quoted or indirectly quoted someone who > > > said : > > > >>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.. > > > (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.- 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
From: Martin Gregorie on 11 Jun 2010 14:22 On Fri, 11 Jun 2010 06:53:14 -0700, eunever32(a)yahoo.co.uk wrote: >> > (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. >> 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. >> 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 >> >> } >> 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 |
From: Lothar Kimmeringer on 11 Jun 2010 16:27 Roedy Green wrote: > On Thu, 10 Jun 2010 02:04:40 -0700 (PDT), "eunever32(a)yahoo.co.uk" > <eunever32(a)yahoo.co.uk> wrote, quoted or indirectly quoted someone who > said : > >> >>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. 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: spamfang(a)kimmeringer.de PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81) Always remember: The answer is forty-two, there can only be wrong questions!
From: eunever32 on 13 Jun 2010 08:31 On Jun 11, 7:22 pm, Martin Gregorie <mar...(a)address-in-sig.invalid> wrote: > On Fri, 11 Jun 2010 06:53:14 -0700, euneve...(a)yahoo.co.uk wrote: > >> > (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. > > 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. > > > > > > >> 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 > > >> } > > 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.
From: Martin Gregorie on 13 Jun 2010 12:20 On Sun, 13 Jun 2010 05:31:25 -0700, eunever32(a)yahoo.co.uk wrote: > On Jun 11, 7:22 pm, Martin Gregorie <mar...(a)address-in-sig.invalid> > wrote: >> On Fri, 11 Jun 2010 06:53:14 -0700, euneve...(a)yahoo.co.uk wrote: >> >> > (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. >> >> 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. >> >> >> >> >> >> >> 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 >> >> >> } >> >> 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 > 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 |
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Java VM 1.6.0 Sorting Collections Next: Best way to halt Java process? |