protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session, request.getInputStream()); Address[] from = message.getFrom(); String fromAddress = ""; if (from.length > 0) { fromAddress = from[0].toString(); } Object content = message.getContent(); String contentText = ""; if (content instanceof String) { contentText = (String)content; } else if (content instanceof Multipart) { Multipart multipart = (Multipart)content; Part part = multipart.getBodyPart(0); Object partContent = part.getContent(); if (partContent instanceof String) { contentText = (String)partContent; } } logger.info("Received email from=["+fromAddress+"] subject=["+message.getSubject()+"]"); logger.info("Content: "+contentText); return null; }
The key to this fix is that the calls to getContent() now return the correct object type. So in my case I'm only interested in text/plain and therefore just Strings.
Sadly JAXB still doesn't work so I'll continue to use Simple XML and wait for 1.2.9.
UPDATE: JAXB support was fixed on December 10th without updating the version number.
Me who thought I was doing something wrong with the mail API... and it was just a bug in the SDK! Thanks for this post!
ReplyDelete