7 Replies Latest reply: Dec 30, 2011 11:05 PM by mdroid RSS

    Recover attactments with JavaMail in Android

    mdroid
      Hello,

      I used Oracle JavaMail 1.4.4 + Oracle JAF 1.1.1 and additional.jar (javax.awt dependencies replacement) from [url http://code.google.com/p/javamail-android/] javamail-android

      Me neither sounds good, but the only way I found to make it work, for the time being sent without problems.

      But I have a problem retrieving attachments, and that is the code that I used before, now is not Mutipart instance.

      This is the code:
      SubjectTerm term = new SubjectTerm("[aNAME]");
                                    messages = inbox.search(term);
                                          
                                    messages = ufolder.getMessagesByUID(1, UIDFolder.LASTUID);
      
                                  // FetchProfile
                                  FetchProfile fp = new FetchProfile();
                                  fp.add(FetchProfile.Item.ENVELOPE);
                                  fp.add(FetchProfile.Item.FLAGS);
                                  fp.add("X-Mailer");
                                  inbox.fetch(messages, fp);
                                    
                                    for (int n = 0; n < messages.length; n++) 
                                    {
                                         message = messages[n];
                                         uid = ufolder.getUID(message);
                                              
                                              messagecontentObject = message.getContent();
                                              
                                                   if (messagecontentObject instanceof Multipart)
                                                   {
      When it comes to:
      messagecontentObject = message.getContent();
      
      if (messagecontentObject instanceof Multipart)
      this is null (messagecontentObject) therefore can not go inside the if statement

      This a debug trace:
      12-27 19:39:44.419: I/System.out(4941): DEBUG: setDebug: JavaMail version 1.4.4
      12-27 19:39:44.419: I/System.out(4941): DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc.,1.4.4]
      12-27 19:39:44.429: I/System.out(4941): DEBUG: mail.imap.fetchsize: 16384
      12-27 19:39:44.429: I/System.out(4941): DEBUG: mail.imap.statuscachetimeout: 1000
      12-27 19:39:44.429: I/System.out(4941): DEBUG: mail.imap.appendbuffersize: -1
      12-27 19:39:44.429: I/System.out(4941): DEBUG: mail.imap.minidletime: 10
      12-27 19:39:44.429: I/System.out(4941): DEBUG: trying to connect to host "imap.gmail.com", port 993, isSSL false
      12-27 19:39:44.759: D/dalvikvm(4941): GC_CONCURRENT freed 454K, 53% free 2849K/5959K, external 1019K/1531K, paused 4ms+5ms
      12-27 19:39:45.069: I/System.out(4941): * OK Gimap ready for requests from 78.107.207.948 ez12if16855419wid.4
      12-27 19:39:45.069: I/System.out(4941): A0 CAPABILITY
      12-27 19:39:45.169: I/System.out(4941): * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH
      12-27 19:39:45.169: I/System.out(4941): A0 OK Thats all she wrote! ez11if16255419wid.4
      12-27 19:39:45.169: I/System.out(4941): DEBUG IMAP: AUTH: XOAUTH
      12-27 19:39:45.169: I/System.out(4941): DEBUG: protocolConnect login, host=imap.gmail.com, user=myuser@gmail.com, password=<non-null>
      12-27 19:39:45.179: I/System.out(4941): A1 LOGIN myuser@gmail.com myuser01
      12-27 19:39:45.401: I/System.out(4941): * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
      12-27 19:39:45.401: I/System.out(4941): A1 OK myuser@gmail.com Bob Esponja authenticated (Success)
      12-27 19:39:45.401: I/System.out(4941): A2 CAPABILITY
      12-27 19:39:45.509: I/System.out(4941): * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
      12-27 19:39:45.509: I/System.out(4941): A2 OK Success
      12-27 19:39:45.529: I/System.out(4941): A3 LIST "" INBOX
      12-27 19:39:45.639: I/System.out(4941): * LIST (\HasNoChildren) "/" "INBOX"
      12-27 19:39:45.639: I/System.out(4941): A3 OK Success
      12-27 19:39:45.639: I/System.out(4941): DEBUG: connection available -- size: 1
      12-27 19:39:45.639: I/System.out(4941): A4 SELECT INBOX
      12-27 19:39:45.769: I/System.out(4941): * FLAGS (\Answered \Flagged \Draft \Deleted \Seen $Forwarded NonJunk)
      12-27 19:39:45.769: I/System.out(4941): * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $Forwarded NonJunk \*)] Flags permitted.
      12-27 19:39:45.769: I/System.out(4941): * OK [UIDVALIDITY 659007327] UIDs valid.
      12-27 19:39:45.769: I/System.out(4941): * 3 EXISTS
      12-27 19:39:45.769: I/System.out(4941): * 0 RECENT
      12-27 19:39:45.769: I/System.out(4941): * OK [UIDNEXT 50] Predicted next UID.
      12-27 19:39:45.769: I/System.out(4941): A4 OK [READ-WRITE] INBOX selected. (Success)
      12-27 19:39:45.779: I/System.out(4941): A5 SEARCH SUBJECT [AppName] ALL
      12-27 19:39:45.902: I/System.out(4941): * SEARCH 1 2 3
      12-27 19:39:45.902: I/System.out(4941): A5 OK SEARCH completed (Success)
      12-27 19:39:45.902: I/System.out(4941): A6 UID FETCH 1:* (UID)
      12-27 19:39:46.019: I/System.out(4941): * 1 FETCH (UID 47)
      12-27 19:39:46.039: I/System.out(4941): * 2 FETCH (UID 48)
      12-27 19:39:46.069: I/System.out(4941): * 3 FETCH (UID 49)
      12-27 19:39:46.089: I/System.out(4941): A6 OK Success
      12-27 19:39:46.099: D/dalvikvm(4941): GC_CONCURRENT freed 453K, 53% free 2838K/5959K, external 1019K/1531K, paused 4ms+7ms
      12-27 19:39:46.109: I/System.out(4941): A7 FETCH 1:3 (ENVELOPE INTERNALDATE RFC822.SIZE FLAGS BODY.PEEK[HEADER.FIELDS (X-Mailer)])
      12-27 19:39:46.259: I/System.out(4941): * 1 FETCH (RFC822.SIZE 31979 INTERNALDATE "27-Dec-2011 16:44:33 +0000" FLAGS (\Seen) ENVELOPE ("Tue, 27 Dec 2011 16:44:17 +0000 (GMT+00:00)" "[AppName] Prueba01" ((NIL NIL "user" "mydomain.com")) ((NIL NIL "user" "mydomain.com")) ((NIL NIL "user" "mydomain.com")) ((NIL NIL "myuser" "gmail.com")) NIL NIL NIL "<1156679744.3.1325004257360.JavaMail.javamailuser@localhost>") BODY[HEADER.FIELDS (X-Mailer)] {4}
      12-27 19:39:46.259: I/System.out(4941): 
      12-27 19:39:46.259: I/System.out(4941): 
      12-27 19:39:46.259: I/System.out(4941): )
      12-27 19:39:46.259: I/System.out(4941): * 2 FETCH (RFC822.SIZE 6817 INTERNALDATE "27-Dec-2011 16:49:59 +0000" FLAGS (\Seen) ENVELOPE ("Tue, 27 Dec 2011 16:49:05 +0000 (GMT)" "[AppName] test1" ((NIL NIL "user" "mydomain.com")) ((NIL NIL "user" "mydomain.com")) ((NIL NIL "user" "mydomain.com")) ((NIL NIL "myuser" "gmail.com")) NIL NIL NIL "<1079240688.1.1325004546463.JavaMail.javamailuser@localhost>") BODY[HEADER.FIELDS (X-Mailer)] {4}
      12-27 19:39:46.259: I/System.out(4941): 
      12-27 19:39:46.259: I/System.out(4941): 
      12-27 19:39:46.259: I/System.out(4941): )
      12-27 19:39:46.269: I/System.out(4941): * 3 FETCH (RFC822.SIZE 8063 INTERNALDATE "27-Dec-2011 16:51:14 +0000" FLAGS (\Seen) ENVELOPE ("Tue, 27 Dec 2011 16:50:21 +0000 (GMT)" "[AppName] test2" ((NIL NIL "user" "mydomain.com")) ((NIL NIL "user" "mydomain.com")) ((NIL NIL "user" "mydomain.com")) ((NIL NIL "myuser" "gmail.com")) NIL NIL NIL "<1079847320.3.1325004621240.JavaMail.javamailuser@localhost>") BODY[HEADER.FIELDS (X-Mailer)] {4}
      12-27 19:39:46.269: I/System.out(4941): 
      12-27 19:39:46.269: I/System.out(4941): 
      12-27 19:39:46.269: I/System.out(4941): )
      12-27 19:39:46.269: I/System.out(4941): A7 OK Success
      12-27 19:39:46.279: I/System.out(4941): A8 FETCH 1 (BODYSTRUCTURE)
      12-27 19:39:46.409: I/System.out(4941): * 1 FETCH (BODYSTRUCTURE (("APPLICATION" "OCTET-STREAM" ("NAME" "video.mp4") NIL NIL "BASE64" 30324 NIL ("ATTACHMENT" ("FILENAME" "video.mp4")) NIL)("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 0 0 NIL NIL NIL) "MIXED" ("BOUNDARY" "----=_Part_2_1157256384.1325004257065") NIL NIL))
      12-27 19:39:46.409: I/System.out(4941): A8 OK Success
      12-27 19:39:46.409: I/System.out(4941): A9 FETCH 2 (BODYSTRUCTURE)
      12-27 19:39:46.549: I/System.out(4941): * 2 FETCH (BODYSTRUCTURE (("APPLICATION" "OCTET-STREAM" ("NAME" "video.mp4") NIL NIL "BASE64" 5172 NIL ("ATTACHMENT" ("FILENAME" "video.mp4")) NIL)("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 0 0 NIL NIL NIL) "MIXED" ("BOUNDARY" "----=_Part_0_1079182336.1325004545751") NIL NIL))
      12-27 19:39:46.549: I/System.out(4941): A9 OK Success
      12-27 19:39:46.549: I/System.out(4941): A10 FETCH 3 (BODYSTRUCTURE)
      12-27 19:39:46.682: I/System.out(4941): * 3 FETCH (BODYSTRUCTURE (("APPLICATION" "OCTET-STREAM" ("NAME" "video.mp4") NIL NIL "BASE64" 6420 NIL ("ATTACHMENT" ("FILENAME" "video.mp4")) NIL)("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 0 0 NIL NIL NIL) "MIXED" ("BOUNDARY" "----=_Part_2_1079188064.1325004621148") NIL NIL))
      12-27 19:39:46.682: I/System.out(4941): A10 OK Success
      12-27 19:39:46.689: I/System.out(4941): A11 CLOSE
      12-27 19:39:46.819: I/System.out(4941): A11 OK Returned to authenticated state. (Success)
      12-27 19:39:46.819: I/System.out(4941): DEBUG: added an Authenticated connection -- size: 1
      12-27 19:39:46.819: I/System.out(4941): A12 LOGOUT
      12-27 19:39:46.939: I/System.out(4941): * BYE LOGOUT Requested
      12-27 19:39:46.939: I/System.out(4941): A12 OK 73 good day (Success)
      12-27 19:39:46.939: I/System.out(4941): DEBUG: IMAPStore connection dead
      12-27 19:39:46.939: I/System.out(4941): DEBUG: IMAPStore cleanup, force false
      12-27 19:39:46.939: I/System.out(4941): DEBUG: IMAPStore cleanup done
      Please, somebody could give me a clue as to why the content of the message is null?.

      Thanks in advance

      Best regards
        • 1. Re: Recover attactments with JavaMail in Android
          Bill Shannon-Oracle
          Android isn't really Java, so I have no idea what might be going wrong there.
          Can you reproduce the problem using real Java?

          Have you looked at the JavaMail FAQ and the demo programs that come with JavaMail?
          • 2. Re: Recover attactments with JavaMail in Android
            mdroid
            Hi bshannon,

            I tryed with search.java, msgshow.java and uidmsgshow.java and all works fine. (with Oracle mail.jar and Oracle activation.jar (1.1.1) and Oracle mail.jar and NO Official activation.jar).

            This a output of search.java:



            FOUND 2 MESSAGES
            --------------------------
            MESSAGE #1:
            FROM: memail@gmail.com
            TO: to@gmail.com
            SUBJECT: [aName] test1
            SendDate: 27-dic-2011 18:27:36
            FLAGS = \Seen
            CONTENT-TYPE: multipart/mixed;
                 boundary="----=_Part_0_1080077376.1325010456162"
            This is a Multipart
            CONTENT-TYPE: application/octet-stream; name=michel_telo.mp4;
                 filename=michel_telo.mp4
            This is just an input stream

            ------------------------------------------------

            MESSAGE #2:
            FROM: memail@gmail.com
            TO: to@gmail.com
            SUBJECT: [aName] test2
            SendDate: 27-dic-2011 18:28:03
            FLAGS = \Seen
            CONTENT-TYPE: multipart/mixed;
                 boundary="----=_Part_2_1079162816.1325010483402"
            This is a Multipart
            CONTENT-TYPE: application/octet-stream; name=michel_telo.mp4
                 filename=michel_telo.mp4
            This is just an input stream

            --------------------------------------

            Find the multipart Content-Type: multipart / mixed, which I see a difference in the code is that in the example of the demo (search.java) uses:
            Object o = p.getContent()
            
                if ( o instanceof Multipart) {
            Gets the content to compare from the object Part (p), and I in my code, I'm trying to get the content from the selected message.

            I'm not sure if this applies in JavaMail 1.4.4 version or not. ¿?
            Object messagecontentObject = message.getContent();
               
                 if ( messagecontentObject instanceof Multipart) {
            Try to edit my code to try to get the content from the object part, to see if that works.

            Thanks,

            greetings
            • 3. Re: Recover attactments with JavaMail in Android
              Bill Shannon-Oracle
              A Message is a Part, so both approaches are equivalent. (Although your code is only handling the simple cases.)

              If it works correctly with real Java and doesn't work with Android, that problem is probably with Android.
              • 4. Re: Recover attactments with JavaMail in Android
                mdroid
                Hi,

                Thanks for yours reply,

                When i debug the both codes i see that Object o is diferent:

                In search.java o = MimeMultiPart(id = 21 )

                In my code o = IMAPInputStream (id = 830085689336 )


                Why does my code o = IMAPInputStream and is not a MimeMultipart?

                Can be a problem for android or my code?

                I would say my code, because there are android mail clients that work well, and I guess they are made with javamail.

                Best regards,
                • 5. Re: Recover attactments with JavaMail in Android
                  mdroid
                  Hello,
                  // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added. 
                      MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
                      mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
                      mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
                      mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
                      mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
                      mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
                      CommandMap.setDefaultCommandMap(mc); 
                    } 
                  From: http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android
                  • 6. Re: Recover attactments with JavaMail in Android
                    Bill Shannon-Oracle
                    If that fixes the problem, then the problem is that the class loader in Android isn't working properly.
                    Or maybe it's just not working the way a Java class loader should work since it's not really Java.
                    • 7. Re: Recover attactments with JavaMail in Android
                      mdroid
                      Solved.

                      In LogCat now say " This is a Multipart"

                      I find it this year :)

                      Thanks very much for all

                      Happy new year!