我想閱讀我的郵件,我可以讓它打印標題,但是從內容和內容顯示有趣。 這裏是我用來顯示消息的代碼: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);
}
}
}
謝謝!
因爲'getFrom'不返回一個字符串?從未使用此API,它看起來像返回一個InternetAddress(可能)有一些訪問者來獲取全名和電子郵件地址。 –