簡體   English   中英

閱讀從GMail發送的郵件

[英]Reading mails sent from GMail

我正在使用JavaMail在Android應用程序中閱讀郵件。 我試圖涵蓋所有組合,即在“自定義服務器” /“ Gmail ID” /“ Live ID”上發送/接收的郵件。

從GMail WITH Attachment發送的某些郵件中出現問題。 我能夠收到附件,但是內容返回javax.mail.internet.MimeMultipart@44f2e698

這是用於接收和讀取消息的代碼:

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

我對有問題的郵件的了解:

  • getFrom還返回名稱,即以這種格式出現FirstName LastName &ltemailID@gmail.com&gt

  • MultiPart包含2個BodyPart:

    • BodyPart 1將內容返回為javax.mail.internet.MimeMultipart@44f2e698

    • BodyPart 2返回正確的附件名稱

BodyPart 1將內容返回為javax.mail.internet.MimeMultipart@44f2e698

嘗試在MimeMultiPart上調用getBodyPart

這可能會返回一個MimeBodyPart,您可以在http://docs.oracle.com/javaee/5/api/javax/mail/internet/MimeBodyPart.html#content上調用getContent()

您可能只處理帶有附件的短信的最簡單情況。 MIME允許更多。 您需要了解multipart / mixed,multipart / alternative,multipart / related和multipart / signed之間的區別。 JavaMail FAQ提供了有關處理附件的更多信息,並且JavaMail下載捆綁包中包含的msgshow.java演示程序展示了如何處理具有嵌套多部分的消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM