Parsing bounced emails using Javamail and DSN.jar

I found the way to redirect my bounced emails using my own smtp server. Now, as per the requirement, I should be able to read the bounced emails using my program, like reason for bouncing, receiver’s email address, email content and etc. Stackoverflow suggested that dsn.jar might be helpful. I saw it has some methods. But I don’t find any examples to check how it is working. Here is the way I am redirecting the bounced emails, my question is how to add a functionality to read the bounced emails here inside/outside the following program ? Please help.

import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.dsn.DeliveryStatus;
import com.sun.mail.dsn.DispositionNotification;
import com.sun.mail.dsn.MessageHeaders;
import com.sun.mail.dsn.MultipartReport;
import com.sun.mail.dsn.Report;

public class SendEmail {
   public static void main(String[] args) throws Exception {
     Properties properties=new Properties();
     InputStream input=new FileInputStream("");
      //String smtpServer = "";
      String smtpServer = "Server.Address";
      int port = 25;
      final String userid = "";
      final String password = properties.getProperty("EMAIL_PASSWORD1");
      String contentType = "text/html";
      String subject = "test: bounce an email to a different address " +
                "from the sender";
      String to = "";//some invalid address
      String bounceAddr = "";//change accordingly
      String body = "Test: get message to bounce to a separate email address";
      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("", smtpServer);
      props.put("mail.smtp.port", "port");
      props.put("mail.transport.protocol", "smtp");
      props.put("mail.smtp.from", bounceAddr);
      Session mailSession = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(userid, password);
     MimeMessage message = new MimeMessage(mailSession);
      //SMTPMessage message=new SMTPMessage(mailSession);
      message.setRecipients(Message.RecipientType.TO, to);
      //message.setHeader("Return-path", bounceAddr);
      message.setContent(body, contentType);
      Transport transport = mailSession.getTransport();
      try {
         System.out.println("Sending ....");
         transport.connect(smtpServer, port, userid, password);
         System.out.println("Sending done ...");
      } catch (Exception e) {
         System.err.println("Error Sending: ");
      MultipartReport multireport = (MultipartReport)message.getContent();
      Report report=new Report(multireport);*/
     /* DeliveryStatus status=new DeliveryStatus();
      DispositionNotification notification=new DispositionNotification();
      MessageHeaders headers=new MessageHeaders();
      MultipartReport multiReport=new MultipartReport();
      Report report=new Report();*/

    /*  if (message.isMimeType("multipart/report")) {
          System.out.println("Inside the loop");
          MultipartReport report = (MultipartReport)message.getContent();
          // see com.sun.mail.dsn package javadocs for MutlipartReport
          MessageHeaders header=new MessageHeaders();
         // header.getRecipients(arg0);


Reading a bounced message is like reading any other message – you have to connect to the Store, open the Folder, and read the Message. Once you have a Message object that represents a bounced message, you can use the kind of code you have commented out above to process the content of that message. But note that the Message object will not be the Message object you sent, but rather a completely different Message object that comes from the Folder associated with the account.

Source: stackoverflow