Javamail read multipart emails
        Posted  
        
            by Hectai
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Hectai
        
        
        
        Published on 2010-04-13T17:29:59Z
        Indexed on 
            2010/04/13
            17:33 UTC
        
        
        Read the original article
        Hit count: 646
        
Hey!
My code is below, and the problem is that some of the emails i get from my POP3 account cannot be read, but with a simple unix email client it can be. It would be very helpful if you can find me what the problem is, or give me some advices.
ps.: yes, i have read the tutorials already.
String[] messageText = new String[500];
Store store = session.getStore("pop3");
            store.connect(host, username, password);
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            Message[] messages = inbox.getMessages();          
            for(int i = 0; i < inbox.getMessageCount() ; i++)
            {
                Message m = messages[i];
                Object o = m.getContent();
                if (o instanceof String) messageText[i] = (String)o;
                else if(o instanceof Multipart)
                {
                    Multipart mp = (Multipart)o;
                    int count = mp.getCount();
                    for(int j = 0; j < count; j++)
                    {
                        BodyPart b = mp.getBodyPart(j);
                        String disposition = b.getDisposition();
                        if (disposition != null && disposition.equals(BodyPart.ATTACHMENT)))
                        {
                            DataHandler handler = b.getDataHandler();
                            messageText[i] += "file name : " + handler.getName();
                        } else
                        {
                            messageText[i] += b.getContent();
                        }
                    }
                 }
                }
                store.close();
        }
        catch(Exception ex)
        {
            notif("Failure    -  " + ex.getMessage());
        }
        © Stack Overflow or respective owner