How to use Papercut with java mail API

Are you looking for a way to use Papercut with java mail API? If so, then I will share the method I stumbled upon while searching for the same.

As a software developer, I too got stuck with this problem. I had to check and verify whether the mail content is in place while sending a mail using JAVA mail API. During Development phase we cannot send the mail directly to someone. We have to make sure that the mail is presentable to the end user. The code for java mail is as shown below

// File Name
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail {

   public static void main(String [] args) {    
      // Recipient's email address.
      String to = "[email protected]";

      // Sender's email address
      String from = "[email protected]";

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("", host);

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try {
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("Whatever subject this is about..");

         // Now set the actual message
         message.setText("Whatever content..");

         // Send message
         System.out.println("Message sent successfully....");
      }catch (MessagingException mex) {

The code above can be altered as per your need. The mail is sent flawlessly, when the following line of code executes.


But we need to verify the formatting. For this, just install the Papercut server.
After installing the server you have to include one more line of code to the java mailing code.
Change the code to

//Assuming you are sending email from localhost.
   String host = "localhost";
   // Assuming you are sending email from localhost.
   String port = "25";
   Properties properties = System.getProperties();
   // Setup mail server
   properties.setProperty(, host);
   // Setup mail server.
   properties.setProperty(mail.smtp.port, port);

The default port for Papercut is set as 25. That’s why we are setting the port as 25 so that all the mail which we are sending from java mail will go to this Papercut server and we can view the mail in this.

Also Read:  Java Compile Time And Run Time Dependencies, Provided Scope And More

You have to make sure that this code should be used only during the time of development. You have to remove the port which we set in the java mail code to get it to work. Hope this helps.

Share this article:

Stay in touch!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Technicles will use the information you provide on this form to be in touch with you and to provide updates and marketing.