0

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??

sumitarora
  • 580
  • 4
  • 18

2 Answers2

1

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

Answered how, here: Decode Base64 data in Java

Community
  • 1
  • 1
Cipi
  • 11,055
  • 9
  • 47
  • 60
  • 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 Jan 22 '10 at 09:55
  • 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 Jan 22 '10 at 10:00
1

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
  • 74,484
  • 29
  • 137
  • 169