Debug a JavaMail ProgramTag(s): Networking


JavaMail Debug mode
To set the JavaMail Debug mode "on" :
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
or set the property when launching the JVM
java -Dmail.debug=true ...
This setting puts the JavaMail classes in debug mode mode to System.out.

To redirect the JavaMail debugging output to a more appropriate log file you can

  • link a PrintStream to a ByteArrayOutputStream,
  • tell to JavaMail to use your PrintStream,
  • do the JavaMail stuff,
  • dump the the content of the ByteArrayOutputStream to your favorite logger.
   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.setDebug(true);
        mailSession.setDebugOut(ps);
     }
     ...
     transport.close();
     if (MAIL_DEBUG) { logger.info(os); }
   }
   finally {
     ps.close();
     os.close();
   }
Verify connectivity to the MailServer with Telnet :
telnet mymailserver 25
for example, you can detect if your firewall is blocking your connection.

Windows 7
By default, the telnet client is not installed on a Win7 workstation. To installed it, open command shell and type :

pkgmgr /iu:"TelnetClient"
You can also install it through the Control Panel, see Microsoft Technet.
Use a JavaMail server mock-up to act as "in-memory" mail server
See https://java.net/projects/mock-javamail and http://quintanasoft.com/dumbster/. These mock-ups are designed to act a mail server but the actual email is not delivered to the mail recipient which can be useful in a testing stage.
blog comments powered by Disqus