简体   繁体   English

阅读从GMail发送的邮件

[英]Reading mails sent from GMail

I am using JavaMail to read mail in my Android app. 我正在使用JavaMail在Android应用程序中阅读邮件。 I have tried to cover all combinations ie Mail sent/received on/from Custom Server/Gmail ID/Live ID. 我试图涵盖所有组合,即在“自定义服务器” /“ Gmail ID” /“ Live ID”上发送/接收的邮件。

The problem occurs with SOME of the mails sent from GMail WITH Attachment. 从GMail WITH Attachment发送的某些邮件中出现问题。 I am able to receive the attachment, but the content returns javax.mail.internet.MimeMultipart@44f2e698 我能够收到附件,但是内容返回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 ie it comes in this format FirstName LastName &ltemailID@gmail.com&gt getFrom还返回名称,即以这种格式出现FirstName LastName &ltemailID@gmail.com&gt

  • MultiPart contains 2 BodyParts: MultiPart包含2个BodyPart:

    • BodyPart 1 returns the content as javax.mail.internet.MimeMultipart@44f2e698 BodyPart 1将内容返回为javax.mail.internet.MimeMultipart@44f2e698

    • BodyPart 2 returns the correct name for attachment BodyPart 2返回正确的附件名称

BodyPart 1 returns the content as javax.mail.internet.MimeMultipart@44f2e698 BodyPart 1将内容返回为javax.mail.internet.MimeMultipart@44f2e698

Try calling getBodyPart on the MimeMultiPart 尝试在MimeMultiPart上调用getBodyPart

That probably returns a MimeBodyPart you can call getContent() on http://docs.oracle.com/javaee/5/api/javax/mail/internet/MimeBodyPart.html#content 这可能会返回一个MimeBodyPart,您可以在http://docs.oracle.com/javaee/5/api/javax/mail/internet/MimeBodyPart.html#content上调用getContent()

You're probably only handling the simplest case of a text message with attachments. 您可能只处理带有附件的短信的最简单情况。 MIME allows much more. MIME允许更多。 You need to learn about the difference between multipart/mixed, multipart/alternative, multipart/related, and multipart/signed. 您需要了解multipart / mixed,multipart / alternative,multipart / related和multipart / signed之间的区别。 The JavaMail FAQ has more information on handling attachments and the msgshow.java demo program included with the JavaMail download bundle shows how to process messages with nested multiparts. JavaMail FAQ提供了有关处理附件的更多信息,并且JavaMail下载捆绑包中包含的msgshow.java演示程序展示了如何处理具有嵌套多部分的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM