JavaMail setDebug (true)

How can I setDebug(true) in a JavaMail session, but capture the stream and use it in my logging system? (Except for loading the source, changing the method to accept the stream as a parameter, recompiling it, ...)

More generally, is there a standard way in Java to generate a shared stream in hijack-and-redirect this way? Note that System.out may already be polluted with another output, and I have no idea how to "filter" this ...

+8
java stream javamail
source share
1 answer

You can link PrintStream to ByteArrayOutputStream, tell JavaMail to use PrintStream, make JavaMail stuff, and finally upload the contents of ByteArrayOutputStream to your favorite journal.

  ByteArrayOutputStream os = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(os); Session mailSession = Session.getDefaultInstance(props, null); try { if (MAIL_DEBUG) { logger.info("JAVAMAIL debug mode is ON"); mailSession.setDebugOut(ps); mailSession.setDebug(true); } ... transport.close(); if (MAIL_DEBUG) { logger.info(os); } } finally { ps.close(); os.close(); } 
+9
source share

All Articles