Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

java - Reading mails sent from GMail

I am using JavaMail to read mail in my Android app. I have tried to cover all combinations i.e Mail sent/received on/from Custom Server/Gmail ID/Live ID.

The problem occurs with SOME of the mails sent from GMail WITH Attachment. I am able to receive the attachment, but the content returns javax.mail.internet.MimeMultipart@44f2e698

Here's the code used to receive and read messages:

    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imap");

    try {
     /* Create the session and get the store for read the mail. */
     Session session = Session.getInstance(props, null);
     Store store = session.getStore("imaps");
     store.connect("imap.gmail.com", Username, Password);

     /* Mention the folder name which you want to read. */
     Folder inbox = store.getFolder("INBOX");
     System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount());         

     /* Open the inbox using store. */
     inbox.open(Folder.READ_ONLY);

     Message messages[] = inbox.getMessages();       
     Log.d("Inbox", "Message Count: "+inbox.getMessageCount());

     for (int i = messages.length - 1 ; i > 0; --i) {
         Log.i("ContentType", "ContentType: "+messages[i].getContentType());

         Object msgContent = messages[i].getContent();

         String content = "";

         /* Check if content is pure text/html or in parts */            
         if (msgContent instanceof Multipart) {

             Multipart multipart = (Multipart) msgContent;

             Log.e("BodyPart", "MultiPartCount: "+multipart.getCount());

             for (int j = 0; j < multipart.getCount(); j++) {

              BodyPart bodyPart = multipart.getBodyPart(j);

              String disposition = bodyPart.getDisposition();

              if (disposition != null && (disposition.equalsIgnoreCase("ATTACHMENT"))) { // BodyPart.ATTACHMENT doesn't work for gmail
                  System.out.println("Mail have some attachment");

                  DataHandler handler = bodyPart.getDataHandler();
                  System.out.println("file name : " + handler.getName());                                 
                }
              else { 
                  System.out.println("Content: "+bodyPart.getContent());
                  content= bodyPart.getContent().toString();
                }
            }
         }
         else                
             content= messages[i].getContent().toString();

What I know about the problematic mails:

  • getFrom also return the name i.e it comes in this format FirstName LastName &ltemailID@gmail.com&gt

  • MultiPart contains 2 BodyParts:

    • BodyPart 1 returns the content as javax.mail.internet.MimeMultipart@44f2e698

    • BodyPart 2 returns the correct name for attachment

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

BodyPart 1 returns the content as javax.mail.internet.MimeMultipart@44f2e698

Try calling getBodyPart on the MimeMultiPart

That probably returns a MimeBodyPart you can call getContent() on http://docs.oracle.com/javaee/5/api/javax/mail/internet/MimeBodyPart.html#content


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...