我能够正确获取 from 字段和主题字段,但是即使我使用 toString() 方法,我也会以对象格式获取邮件内容。
请检查以下源代码
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "mymail@gmail.com", "****");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
Object obj = msg.getContent();
//Multipart mp = (Multipart)obj;
Multipart mp = (Multipart) msg.getContent();
// MimeBodyPart part = (MimeBodyPart)mp.getBodyPart(0);
BodyPart bp = ((Multipart) msg.getContent()).getBodyPart(0);
// Object body = msg.getContent();
//String value = String.valueOf(body);
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + bp.getContent().toString());
} catch (Exception mex) {
mex.printStackTrace();
}
}
}
output console:
FROM:Myname <mymail@gmail.com>
SENT DATE:Tue Nov 05 12:28:24 IST 2013
SUBJECT:test
CONTENT:javax.mail.internet.MimeMultipart@5117f31e