0

I am using java mail to get emails from gmail with attachments, the attachment come as String in the content, how I can convert it to file?

Thanks

this what I got

Content :
begin 644 myfile.csv
M(E-T871U<R(L(E-T87)T(BPB4W1A<G0@9&%T92(L(E-T87)T('1I;64B+")%
M;F0B+")%;F0@9&%T92(L(D5N9"!T:6UE(BPB0V%L;&EN9R!C=7-T;VUE<B(L
.................
end

Object content = message.getContent();
            if (content instanceof String) {//here I got the attachment
                System.out.println(content);
            } else if (content instanceof Multipart) {
                Multipart multiPart = (Multipart) content;
                procesMultiPart(multiPart);
            }
  • Take a look at: http://stackoverflow.com/questions/1748183/download-attachments-using-java-mail – eltabo Mar 24 '14 at 12:43
  • the content returned as String I cannot cast it Multipart, and the attachment comes with the content , ... Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart, – user3455386 Mar 24 '14 at 12:57
  • Please post the code where you get the messages. – eltabo Mar 24 '14 at 13:03
  • I updated the question, any advice? – user3455386 Mar 24 '14 at 13:11
  • I found the solution This is the Answer UUEncode uuEncode = new UUEncode(); try { byte[] decode = uuEncode.decode(content); System.out.println(new String(decode)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } – user3455386 Mar 24 '14 at 14:24
  • Ok. Maybe `message.writeTo(System.out);` also works for you. – eltabo Mar 24 '14 at 14:34

1 Answers1

0

Looks like you have a single part message with uuencoded content. If the message was properly formatted with a Content-Transfer-Encoding header of "uuencode", JavaMail would decode it automatically for you. But it looks like it is not. You can decode it yourself using:

InputStream is = MimeUtility.decode(message.getInputStream(), "uuencode");

Then read the input stream to get the decoded data, e.g., to copy it to a file.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40