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:

Jackson Ignore Properties on Marshalling
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
How to Read HTTP Headers in Spring REST Controllers
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Implement Johnson’s Algorithm
Java Program to Perform String Matching Using String Library
Most commonly used String methods in Java
Spring Boot - Zuul Proxy Server and Routing
Java Concurrency Interview Questions and Answers
Python Program to Find LCM
Arrays.asList vs new ArrayList(Arrays.asList())
Java Program to implement Array Deque
Jackson Date
A Guide to JUnit 5 Extensions
Java Program to Decode a Message Encoded Using Playfair Cipher
Python Program to Find ASCII Value of Character
Java Program for Topological Sorting in Graphs
Java Program to Implement IdentityHashMap API
Python Program to Find HCF or GCD
Python Program to Make a Flattened List from Nested List
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Cơ chế Upcasting và Downcasting trong java
Mảng (Array) trong Java
Spring Data Java 8 Support
Prevent Brute Force Authentication Attempts with Spring Security
Python Program to Display Calendar
CharSequence vs. String in Java
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Creating Docker Images with Spring Boot
Python Program to Capitalize the First Character of a String
Spring Boot - Introduction