I am using this piece of code to retrieve the latest email from my Gmail account.
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "<username@gmail.com>", "<password>");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
MimeMessage msg = (MimeMessage) inbox.getMessage(inbox.getMessageCount());
Address[] in = msg.getFrom();
for (Address address : in) {
resp.getWriter().println("FROM:" + address.toString());
}
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
resp.getWriter().println("SENT DATE:" + msg.getSentDate());
resp.getWriter().println("SUBJECT:" + msg.getSubject());
resp.getWriter().println("CONTENT:" + bp.getContent());
} catch (Exception mex) {
mex.printStackTrace();
}
It's working well as a normal Java application. However, when I am using it as a function in my Servlet running on Google App Engine, it only can get the FROM address, the content is null.
msg.getFrom()
works fine but msg.getContent()
only returns null.
Thanks all for any help.