Debug a JavaMail Program

1. 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 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();
   }

2. Verify connectivity to the MailServer with Telnet:

telnet mymailserver 25

For example, you can detect if your firewall is blocking your connection.

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

pkgmgr /iu:"TelnetClient"

You can also install it through the Control Panel, see Microsoft Technet.

Done! Happy Coding!

Related posts:

Java Program to Implement Hash Tables with Double Hashing
Spring Boot Configuration with Jasypt
Java Program to Compare Binary and Sequential Search
Java Program to Implement Cartesian Tree
Hướng dẫn Java Design Pattern – Singleton
Java Program to Implement Binomial Tree
List Interface trong Java
Setting Up Swagger 2 with a Spring REST API
Java Program to Implement WeakHashMap API
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Implement CopyOnWriteArrayList API
Java Program to Implement Suffix Tree
MyBatis with Spring
Spring’s RequestBody and ResponseBody Annotations
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Implement the Checksum Method for Small String Messages and Detect
Spring Boot - Interceptor
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Java Program to Implement Stack API
Spring Boot - Creating Docker Image
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Lớp Properties trong java
Từ khóa throw và throws trong Java
Remove the First Element from a List
Spring Boot Tutorial – Bootstrap a Simple Application
Reading an HTTP Response Body as a String in Java
Filtering a Stream of Optionals in Java
Spring Boot - Database Handling
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Implement Ternary Search Algorithm
Java Program to Implement Sparse Matrix