@Test void wrapShouldPreserveBody() throws Exception { String messageAsText = "header1: <5436@ab.com>\r\n" + "Subject: test\r\n" + "\r\n" + "Content!"; InputStream inputStream = new ByteArrayInputStream(messageAsText.getBytes(StandardCharsets.UTF_8)); MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), inputStream); MimeMessageWrapper mimeMessageWrapper = MimeMessageWrapper.wrap(message); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); mimeMessageWrapper.writeTo(outputStream); assertThat(new String(outputStream.toByteArray(), StandardCharsets.UTF_8)) .isEqualTo(messageAsText); }
public static MimeMessageWrapper wrap(MimeMessage mimeMessage) throws MessagingException { try { return new MimeMessageWrapper(mimeMessage); } catch (MessagingException e) { MimeMessageWrapper result = new MimeMessageWrapper(); ThrowingConsumer<Header> consumer = header -> result.addHeader(header.getName(), header.getValue()); Collections.list(mimeMessage.getAllHeaders()) .forEach(Throwing.consumer(consumer).sneakyThrow()); result.setText(""); return result; } }
MimeMessageWrapper extends MimeMessage { public static MimeMessageWrapper wrap(MimeMessage mimeMessage) throws MessagingException { try { return new MimeMessageWrapper(mimeMessage); } catch (MessagingException e) { MimeMessageWrapper result = new MimeMessageWrapper(); ThrowingConsumer<Header> consumer = header -> result.addHeader(header.getName(), header.getValue()); Collections.list(mimeMessage.getAllHeaders()) .forEach(Throwing.consumer(consumer).sneakyThrow()); result.setText(""); return result; } } }
MimeMessageWrapper extends MimeMessage { public static MimeMessageWrapper wrap(MimeMessage mimeMessage) throws MessagingException { try { return new MimeMessageWrapper(mimeMessage); } catch (MessagingException e) { MimeMessageWrapper result = new MimeMessageWrapper(); ThrowingConsumer<Header> consumer = header -> result.addHeader(header.getName(), header.getValue()); Collections.list(mimeMessage.getAllHeaders()) .forEach(Throwing.consumer(consumer).sneakyThrow()); result.setText(""); return result; } } private  MimeMessageWrapper(); private  MimeMessageWrapper(MimeMessage mimeMessage); }
MimeMessageWrapper extends MimeMessage { public static MimeMessageWrapper wrap(MimeMessage mimeMessage) throws MessagingException { try { return new MimeMessageWrapper(mimeMessage); } catch (MessagingException e) { MimeMessageWrapper result = new MimeMessageWrapper(); ThrowingConsumer<Header> consumer = header -> result.addHeader(header.getName(), header.getValue()); Collections.list(mimeMessage.getAllHeaders()) .forEach(Throwing.consumer(consumer).sneakyThrow()); result.setText(""); return result; } } private  MimeMessageWrapper(); private  MimeMessageWrapper(MimeMessage mimeMessage); static MimeMessageWrapper wrap(MimeMessage mimeMessage); }
MimeMessageWrapper extends MimeMessage { public static MimeMessageWrapper wrap(MimeMessage mimeMessage) throws MessagingException { try { return new MimeMessageWrapper(mimeMessage); } catch (MessagingException e) { MimeMessageWrapper result = new MimeMessageWrapper(); ThrowingConsumer<Header> consumer = header -> result.addHeader(header.getName(), header.getValue()); Collections.list(mimeMessage.getAllHeaders()) .forEach(Throwing.consumer(consumer).sneakyThrow()); result.setText(""); return result; } } private  MimeMessageWrapper(); private  MimeMessageWrapper(MimeMessage mimeMessage); static MimeMessageWrapper wrap(MimeMessage mimeMessage);  }