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 Max-Flow Min-Cut Theorem
Examine the internal DNS cache
Converting Java Date to OffsetDateTime
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java – Write an InputStream to a File
Java Program to Implement a Binary Search Tree using Linked Lists
The XOR Operator in Java
Java Program to Implement Hamiltonian Cycle Algorithm
Java Program to Implement vector
Java Program to Implement Lloyd’s Algorithm
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Java Program to Implement Pagoda
Python Program to Check Whether a String is Palindrome or Not
How to Get the Last Element of a Stream in Java?
Getting Started with Forms in Spring MVC
StringBuilder vs StringBuffer in Java
Java Program to implement Sparse Vector
Java Program to Implement Bubble Sort
Introduction to Apache Commons Text
Consuming RESTful Web Services
HttpClient 4 – Follow Redirects for POST
Adding Shutdown Hooks for JVM Applications
New Features in Java 13
Java Program to Generate Randomized Sequence of Given Range of Numbers
Java Program to Implement Sorted Circular Doubly Linked List
Giới thiệu luồng vào ra (I/O) trong Java
Java Program to implement Bit Set
Encode a String to UTF-8 in Java
Introduction to Spring Security Expressions