views:

112

answers:

2

I'm creating email client, when i receive emails from blackberry server it sends file name as "=?utf-8?B?anBlZ2F0dGFjaG1lbnQuSlBFRw==?=" but the original filename was "jpegattachment.JPEG", and sometime I get the plain text when I receive from other mail servers. So here my problem is I can get a string which may or may not be encoded.

Is there any way, I can get the encoding of string and decode that into plain text.

Either the input string is "=?utf-8?B?anBlZ2F0dGFjaG1lbnQuSlBFRw==?=" or "jpegattachment.JPEG" output should be "jpegattachment.JPEG".

Any Idea??

+1  A: 

Yes, this: anBlZ2F0dGFjaG1lbnQuSlBFRw== is base64 encoded jpegattachment.JPEG. So just decode it with base64.

Answered how, here: http://stackoverflow.com/questions/469695/decode-base64-data-in-java

Cipi
So, I have to manually trim "=?utf-8?B?".I cant trim as in some cases it can be just a plain text.How can I do this on runtime where i can receive plain text or an encoded one?
sumitarora
Well... if you try to decode string that is not base64 it will throw an error, so you can try-catch check the string. And if it doesnt throw any exception during decoding it was base64, and if it did throw an exception it was plain text. But I guess that the "B" in all that stands for Base64-encoded-parameters... (so if there is "B: in the string you can know that what comes between the next two questionmarks is Base64 encoded.)
Cipi
+1  A: 

This is MIME-encoded. Even though Base64 is most popular, it may use other encodings like Quoted-printable, binary etc. So you should use an existing library to decode this. Any mail program will have decoder built-in.

You can use the decodeWord() from Java Mail,

http://java.sun.com/products/javamail/javadocs/javax/mail/internet/MimeUtility.html#decodeWord(java.lang.String)

Try deocdeText() if you want leave plain-text alone.

ZZ Coder
Thanks a lot....
sumitarora