在Android应用程序中从gmail检索电子邮件时出现问题 [英] Problems retrieving emails from gmail in Android app

查看:134
本文介绍了在Android应用程序中从gmail检索电子邮件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用内置于java中的java邮件API从他们的Gmail邮箱中获取用户的邮件,大部分代码位于另一个SO问题。我能够正确地获取大部分信息,比如邮箱中的主题,发件人和其他信息。

I'm using the java mail api built into java to get the mail of a user from their gmail box, most of the code i found in another SO question. And I'm able to pull most of the information i want correctly, like the subject, senders and other info from my mailbox.

一切都很好,除非我去拉动消息的内容,它并不总是拉动消息的内容。只有十分之一的效果。其他9次它只是发现 javax.mail.internet.MimeMultipart@40e9c920

Everything works great except when i go to pull the "content" of the message it doesnt always pull the content of the message. Only like 1 in 10 times it works. The other 9 times it just finds "javax.mail.internet.MimeMultipart@40e9c920"

我的代码如下。我将输出打印到 LogCat 中进行测试。提前致谢。

My code is below. I'm printing the output into LogCat to test. Thanks in advance.

Properties props = System.getProperties();
            Session session = Session.getDefaultInstance(props, null);
            Store store = session.getStore("imaps");
            store.connect("imap.gmail.com", "Email address here",
                    "password here");
            Folder inbox = store.getFolder("Inbox");
            inbox.open(Folder.READ_ONLY);
            Message messages[] = inbox.getMessages();
            for (Message message : messages)
                Log.d("Email", message + "");
            Message message[] = inbox.getMessages();

                    for (int i = 0; i < 25; i++) {
                        Log.d("From", message[i].getFrom()[0] + "");
                        Log.d("Subject", message[i].getSubject() + "");
                        String content = message[i].getContent().toString();
                        Log.d("content", content + "");

                    }

编辑:
经过一些额外的研究,已经发现它与阅读JavaMail的多部分电子邮件有关

After some additional research I've found it has something to do with reading a multipart email with JavaMail

推荐答案

如果其他人有这个问题,一直工作的是,当它发现多部分消息时,它无法读取它们。以下是阅读多部分电子邮件的解决方案。我在本网站上找到了的大部分代码

If anyone else has this problem the reason it wasn't working all the time is that when it found multipart messages it was unable to read them. Below is the solution to read multipart emails. I found most of the code on this website.

String s = message[i].getContent() + "";

                    if(s.indexOf("MimeMultipart") != -1){
                        Multipart multipart = (Multipart) message[i].getContent();

                          for (int x = 0; x < multipart.getCount(); x++) {
                          BodyPart bodyPart = multipart.getBodyPart(x);

                          String disposition = bodyPart.getDisposition();
                          //Log.d("disposition", disposition + "");

                          if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
                              System.out.println("Mail have some attachment : ");

                              DataHandler handler = bodyPart.getDataHandler();
                              System.out.println("file name : " + handler.getName());
                              } else {
                              System.out.println(bodyPart.getContent());
                              }
                              }
                              System.out.println();



                          }

                    else
                        Log.d("Content", message[i].getContent() + "");

                } 

这篇关于在Android应用程序中从gmail检索电子邮件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆