1

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

Community
  • 1
  • 1
Peter
  • 5,071
  • 24
  • 79
  • 115

2 Answers2

3

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

                } 
Peter
  • 5,071
  • 24
  • 79
  • 115
2

Try this,

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

R.daneel.olivaw
  • 2,681
  • 21
  • 31
Lucifer
  • 29,392
  • 25
  • 90
  • 143