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

huangapple go评论81阅读模式
英文:

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

问题

以下是翻译好的内容:

我已经为消息阅读开发了以下代码:

 else if (p.isMimeType("multipart/*")) {
            Log.i("snt", "22");
            System.out.println("这是一个多部分消息");
            System.out.println("---------------------------");
            Multipart mp = (Multipart) p.getContent();
            int count = mp.getCount();
            //for (int i = 0; i < count; i++)
            {
                System.out.println("数量为:" + 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("邮件有附件:");
                        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("文件名:" + 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);
                    }
英文:

I have developed code for Message reading its as follows

 else if (p.isMimeType(&quot;multipart/*&quot;)) {
            Log.i(&quot;snt&quot;, &quot;22&quot;);
            System.out.println(&quot;This is a Multipart&quot;);
            System.out.println(&quot;---------------------------&quot;);
            Multipart mp = (Multipart) p.getContent();
            int count = mp.getCount();
            //for (int i = 0; i &lt; count; i++)
            {
                System.out.println(&quot;Count is : &quot; + String.valueOf(count));
                Multipart multipart = mp;//(Multipart) msg[i].getContent();
                for (int x = 0; x &lt; multipart.getCount(); x++) {
                    BodyPart bodyPart = multipart.getBodyPart(x);
                    Integer ctr = MainActivity.maxSlno();
                    String disposition = bodyPart.getDisposition();
                    if (disposition != null &amp;&amp; (disposition.equals(BodyPart.ATTACHMENT))) {
                        System.out.println(&quot;Mail have some attachment : &quot;);
                        Log.i(&quot;snt&quot;, &quot;33&quot;);
                        DataHandler handler = bodyPart.getDataHandler();
                        File root = Environment.getExternalStorageDirectory();
                        String path = root.getPath() + &quot;/&quot; + handler.getName();//+ &quot;/PSattachment.psz&quot;;
                        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(&quot;file name : &quot; + handler.getName());
                    } else {
                        Log.i(&quot;snt&quot;, &quot;44&quot;);
                        String str;//=bodyPart.toString();//(bodyPart.getContent());
                        str = bodyPart.getContent().toString();
                        Log.i(&quot;snt&quot;, 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.

答案1

得分: 0

问题可能是您没有处理嵌套的多部分内容。请参阅msgshow.java演示程序,了解处理嵌套多部分内容的示例。

英文:

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.

huangapple
  • 本文由 发表于 2020年4月10日 16:34:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61136670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定