Problems retrieving emails from gmail in Android app

℡╲_俬逩灬. 提交于 2019-12-23 03:31:25

问题


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.

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"

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 + "");

                    }

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


回答1:


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() + "");

                } 



回答2:


Try this,

imap.gmail.com replaced by smtp.gmail.com



来源:https://stackoverflow.com/questions/8622877/problems-retrieving-emails-from-gmail-in-android-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!