我正在尝试使用Java从Gmail获取电子邮件。我使用java-gmail-imap库。当我尝试获取message.getContent()时,它始终给出Inputstream。它没有提供足够的信息来解析部分。我的代码看起来像这样。请让我知道我错过了什么?
IMAPStore imapStore = OAuth2Authenticator.connectToImap(
"imap.gmail.com", 993, email, token, false);
Folder folder = imapStore.getFolder("Inbox");
if (folder.isOpen()) {
if ((folder.getMode() & Folder.READ_WRITE) != 0) {
folder.close(false);
folder.open(Folder.READ_ONLY);
}
} else {
try {
folder.open(Folder.READ_ONLY);
} catch (Exception e) {
e.printStackTrace();
}
}
Message[] messages = null;
SearchTerm newerThan = new ReceivedDateTerm(ComparisonTerm.GT, lastDate);
messages = folder.search(newerThan);
FetchProfile fp = new FetchProfile();
fp.add("X-GM-MSGID");
fp.add(UIDFolder.FetchProfileItem.UID);
fp.add(IMAPFolder.FetchProfileItem.X_GM_MSGID);
//fp.add(FetchProfile.Item.ENVELOPE);
//fp.add(FetchProfileItem.FLAGS);
//fp.add(FetchProfileItem.CONTENT_INFO);
// fp.add("X-mailer");
folder.fetch(messages, fp);
for (Message message : messages) {
if (message != null) {
Object o = message.getContent();
if (o instanceof String) {
logger.info("Message content is string");
} else if (o instanceof Multipart) {
logger.info("Message content is multipart");
} else if (o instanceof Message) {
logger.info("Nested Message");
} else if (o instanceof InputStream) {
logger.info("Message content is inputstream");
}
}
}
修改 通过添加mailcap
来解决问题CommandMap.setDefaultCommandMap(new com.google.code.javax.activation.MailcapCommandMap(this.getClass()。getClassLoader()。getResourceAsStream(“mailcap”)));