1
votes

I use javamail to get message, when I get the message i have: com.sun.mail.util.BASE64DecoderStream,

I know that is part of multipart message, in the source of message I have

Content-Type: image/png; name=index_01.png

Content-Transfer-Encoding: base64

How encode this message??

edit: I have that code:

else if (mbp.getContent() instanceof BASE64DecoderStream){
                        InputStream is = null;
                        ByteArrayOutputStream os = null;

                            is = mbp.getInputStream();
                            os = new ByteArrayOutputStream(512);
                            int c = 0;
                            while ((c = is.read()) != -1) {
                                os.write(c);
                            }



                            System.out.println(os.toString()); 

                    }

And that code return strange string, for example: Ř˙á?Exif??II*????????????˙ě?Ducky???????˙á)

3

3 Answers

2
votes

com.sun.mail.util.BASE64DecoderStream is platform dependent. You cannot rely on that always being the type of class that handles base64 decoding.

Rather the javamail APIs already support decoding for you:

// part is instanceof javax.mail.Part
ByteArrayOutputStream bos = new ByteArrayOutputStream();
part.getDataHandler().writeTo(bos);

String decodedContent = bos.toString()
0
votes

What are you expecting when you read the contents of an image part? The image is stored in the message in an encoded format, but JavaMail is decoding the data before returning the bytes to you. If you store the bytes in a file, you can display the image with many image viewing/editing applications. If you want to display them with your Java program, you'll need to convert the bytes to an appropriate Java Image object using (e.g.) APIs in the java.awt.image package.

0
votes

That Sun's base 64 encoder is in the optional package and can be moved or renamed at any time without warning, also may be missing in alternative Java runtimes, also access to these packages may be disabled. It is much better not to rely on it.

I would say, use Base64 from Apache Commons instead, should do the same. Hope you can rebuild and fix the source code.