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:

Spring REST with a Zuul Proxy
A Custom Media Type for a Spring REST API
Auditing with JPA, Hibernate, and Spring Data JPA
Java Program to Implement Efficient O(log n) Fibonacci generator
Queue và PriorityQueue trong Java
Python Program to Differentiate Between type() and isinstance()
Spring Boot Actuator
Python Program to Compute all the Permutation of the String
Java Program to Compute DFT Coefficients Directly
Integer Constant Pool trong Java
Java Program to Represent Linear Equations in Matrix Form
Login For a Spring Web App – Error Handling and Localization
Immutable Map Implementations in Java
StringBuilder vs StringBuffer in Java
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
@Lookup Annotation in Spring
Java Program to Solve Knapsack Problem Using Dynamic Programming
Check if there is mail waiting
Guide to the Java TransferQueue
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
How to Kill a Java Thread
Spring Cloud – Adding Angular
Versioning a REST API
Java Program to Check whether Graph is a Bipartite using DFS
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
String Initialization in Java
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Python Program to Check If a List is Empty
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Optional trong Java 8
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Spring Boot - Quick Start