Search code examples
javaandroidjakarta-mailmultipart

I am developing one email client app for Android. i am having problem in MultiPart message body reading


I have developed code for Message reading its as follows

 else if (p.isMimeType("multipart/*")) {
            Log.i("snt", "22");
            System.out.println("This is a Multipart");
            System.out.println("---------------------------");
            Multipart mp = (Multipart) p.getContent();
            int count = mp.getCount();
            //for (int i = 0; i < count; i++)
            {
                System.out.println("Count is : " + String.valueOf(count));
                Multipart multipart = mp;//(Multipart) msg[i].getContent();
                for (int x = 0; x < multipart.getCount(); x++) {
                    BodyPart bodyPart = multipart.getBodyPart(x);
                    Integer ctr = MainActivity.maxSlno();
                    String disposition = bodyPart.getDisposition();
                    if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
                        System.out.println("Mail have some attachment : ");
                        Log.i("snt", "33");
                        DataHandler handler = bodyPart.getDataHandler();
                        File root = Environment.getExternalStorageDirectory();
                        String path = root.getPath() + "/" + handler.getName();//+ "/PSattachment.psz";
                        File savedFile = new File(path);
                        String destFilePath = path;//.getFileName();
                        FileOutputStream output = new FileOutputStream(destFilePath);
                        InputStream input = bodyPart.getInputStream();
                        byte[] buffer = new byte[4096];
                        int byteRead;
                        while ((byteRead = input.read(buffer)) != -1) {
                            output.write(buffer, 0, byteRead);
                        }
                        output.close();
                        MainActivity.addAttachment(String.valueOf(ctr), path);//handler.getName()
                        System.out.println("file name : " + handler.getName());
                    } else {
                        Log.i("snt", "44");
                        String str;//=bodyPart.toString();//(bodyPart.getContent());
                        str = bodyPart.getContent().toString();
                        Log.i("snt", str);
                        detail_mail.mbody.setText(str);
                    }

Above code is downloading attachments but body of the mail is not being printed. Instead of email body its giving value like javax.mail.internet.MimeMultipart@f3f0d6c. Kindly help me in reading body part Multipart email message. Thank you in advance for your help.


Solution

  • The problem might be that you're not handling nested multiparts. See the msgshow.java demo program for an example of how to handle nested multiparts.