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:

Giới thiệu Aspect Oriented Programming (AOP)
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Validations for Enum Types
Apache Commons Collections OrderedMap
Tiêu chuẩn coding trong Java (Coding Standards)
Spring Boot - Code Structure
Guide to the Java TransferQueue
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Spring Boot - Service Components
Generating Random Numbers in a Range in Java
Object cloning trong java
Java Program to Implement String Matching Using Vectors
Java Program to Implement Shunting Yard Algorithm
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Send an email with an attachment
Java Program to Describe the Representation of Graph using Adjacency List
Java Program to Find Basis and Dimension of a Matrix
Java Program to find the peak element of an array using Binary Search approach
Extract links from an HTML page
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
How to use the Spring FactoryBean?
Spring Boot Change Context Path
Assert an Exception is Thrown in JUnit 4 and 5
Partition a List in Java
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 Cubic convergence 1/pi Algorithm
Java Program to Check whether Directed Graph is Connected using BFS
Spring Boot - Zuul Proxy Server and Routing
Python Program to Create a Countdown Timer
Java Streams vs Vavr Streams
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers