2012-10-20 122 views
5

我想閱讀我的郵件,我可以讓它打印標題,但是從內容和內容顯示有趣。 這裏是我用來顯示消息的代碼:JavaMail檢查郵件內容Gmail IMAP

int j = message.length-1; 
    for (int i=j;i>=0;i--) { 
     System.out.println("Message " + (i + 1)); 
     System.out.println("From : " + message[i].getFrom()); 
     System.out.println("Subject : " + message[i].getSubject()); 
     try { 
      System.out.println("Body: " + message[i].getContent()); 
     } catch (IOException ex) { 
      System.out.println(ex); 
     } 
    } 

輸出如下:

Message 1: 
From: [javax.mail.internet.InternetAddress;@175831e] 
Subject: Hello //This is correct 
Body: [email protected] 

爲什麼沒有這種打印出實際的電子郵件地址從聲明?爲什麼不打印出實際的內容? (我只在純文本有趣。)

整個代碼:

import javax.mail.*; 
import javax.mail.internet.*; 
import javax.mail.search.*; 
import java.util.*; 
import com.sun.mail.imap.*; 
import java.io.*; 

public class MailClient { 
public static void main(String[] args) { 
    try { 
    Properties props = new Properties(); 
    props.put("mail.store.protocol","imaps"); 

    Session session; 

    session = Session.getDefaultInstance(props, null); 
    Store store = session.getStore("imaps"); 
    store.connect("imap.gmail.com","[email protected]","password"); 

    IMAPFolder folder = (IMAPFolder) store.getFolder("inbox"); 
    folder.open(Folder.READ_ONLY); 

    Flags seen = new Flags(Flags.Flag.SEEN); 
    FlagTerm unseenFlagTerm = new FlagTerm(seen,false); 
    Message message[] = folder.search(unseenFlagTerm); 


    int j = message.length-1; 
    for (int i=j;i>=0;i--) { 
     System.out.println("Message " + (i + 1)); 
     System.out.println("From : " + message[i].getFrom()); 
     System.out.println("Subject : " + message[i].getSubject()); 
     try { 
      System.out.println("Body: " + message[i].getContent()); 
     } catch (IOException ex) { 
      System.out.println(ex); 
     } 
    }  
    System.out.println(newMsg); 

    folder.close(false); 
    store.close(); 
    } 
    catch (MessagingException e) { 
     System.out.println("Error: " + e); 
    } 
} 
} 

謝謝!

+1

因爲'getFrom'不返回一個字符串?從未使用此API,它看起來像返回一個InternetAddress(可能)有一些訪問者來獲取全名和電子郵件地址。 –

回答

7

對於純文本和HTML消息:

String content= messages[i].getContent().toString();

多部分消息:

Multipart multipart = (Multipart) messages[i].getContent(); 

    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("Body: "+bodyPart.getContent()); 
       content= bodyPart.getContent().toString(); 
      } 
2

改變這種

message[i].getFrom() 

message[i].getFrom()[0].toString()) 
+0

身體怎麼樣?使用message.getContent()不返回正文。 – eatonphil

0

嘗試下使用:

message[i].writeTo(System.out);