public static String getBody(Message message) throws Exception { try { message.getContent(); // This might throw an exception... } catch (Throwable ex) { try { // Content could not be resolved for some reason - just get the raw stuff return ConvertUtil.inputStreamToString(message.getInputStream()); } catch (Throwable ex2) { // bodystructure is corrupt - get the really raw stuff ByteArrayOutputStream out = new ByteArrayOutputStream(); message.writeTo(out); return out.toString(); } } // check if message is a normal message or a Multipart message if (message.getContent() instanceof MimeMultipart) { MultipartResult res = new MailUtils.MultipartResult(); MailUtils.readMultiPart(res, (MimeMultipart) message.getContent()); return res.body.isEmpty() ? res.bodyHtml : res.body; } else { if (message.getContent().toString() != null) { return message.getContent().toString(); } } return null; }
/** * Helper method for testing which stores a copy of the message locally as the POP3 * * <p>message will be deleted from the server * * @param msg the message to store * @throws IOException If a failure happens writing the message * @throws MessagingException If a failure happens reading the message */ protected void storeMessage(Message msg) throws IOException, MessagingException { if (backupEnabled) { String filename = msg.getFileName(); if (filename == null) { Address[] from = msg.getFrom(); if (from != null && from.length > 0) { filename = from[0] instanceof InternetAddress ? ((InternetAddress) from[0]).getAddress() : from[0].toString(); } else { filename = "(no from address)"; } filename += "[" + UUID.getUUID() + "]"; } filename = FileUtils.prepareWinFilename(filename); filename = backupFolder + filename + ".msg"; if (logger.isDebugEnabled()) { logger.debug("Writing message to: " + filename); } File f = FileUtils.createFile(filename); FileOutputStream fos = new FileOutputStream(f); msg.writeTo(fos); } }
public WOActionResults createDataStore() throws IOException, MessagingException { File emailFile = new File("Resources/largeEmail.eml"); javax.mail.Message message = convertEmlToMessage(emailFile); EOObjectStore osc = new ERXObjectStoreCoordinator(true); EOEditingContext ec = ERXEC.newEditingContext(osc); ec.lock(); try { DataContainer container = (DataContainer) EOUtilities.createAndInsertInstance(ec, DataContainer.class.getSimpleName()); DataStore dataStore = (DataStore) EOUtilities.createAndInsertInstance(ec, DataStore.class.getSimpleName()); dataStore.setDataContainer(container); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); message.writeTo(byteStream); NSData rawEmail = new NSData(byteStream.toByteArray()); dataStore.setData(rawEmail); ec.saveChanges(); } finally { ec.unlock(); ec.dispose(); osc.dispose(); ec = null; osc = null; } return null; }
/** * Retrieve message from retriever and check the body content * * @param retriever Retriever to read from * @param to Account to retrieve */ private void retrieveAndCheckBody(Retriever retriever, String to) throws MessagingException, IOException { Message[] messages = retriever.getMessages(to); assertEquals(1, messages.length); Message message = messages[0]; assertTrue(message.getContentType().equalsIgnoreCase("application/blubb")); // Check content InputStream contentStream = (InputStream) message.getContent(); byte[] bytes = IOUtils.toByteArray(contentStream); assertArrayEquals(createLargeByteArray(), bytes); // Dump complete mail message. This leads to a FETCH command without section or "len" specified. message.writeTo(new ByteArrayOutputStream()); }