Home Javamail How to show all header information of message using Java Mail api



How to show all header information of message using Java Mail api
Posted on: December 19, 2008 at 12:00 AM
This Example shows you how to show all header of any message using javamail api.

How to show all header information of message using Java Mail api

     

This Example shows you how to show all header of any message using javamail api. All messages are stored in Folder objects. folders can contain folders or messages. The Folder class declares methods that fetch, append, copy and delete messages. 

We will use method given below to retrieve all header information, this method returns Enumerations type. 

public Enumeration getAllHeaders();

AllHeaderClient.java

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class AllHeaderClient {

  public static void main(String[] args) throws Exception {
  String host = "192.168.10.205";
  String user = "test";
  String password = "test";

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

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

  // Get a Store object that implements the specified protocol.
  Store store = session.getStore("pop3");

  //Connect to the current host using the specified username and password.
  store.connect(host, user, password);

  //Create a Folder object corresponding to the given name.
  Folder folder = store.getFolder("inbox");

  // Open the Folder.
  folder.open(Folder.READ_ONLY);

  Message[] messages = folder.getMessages();
  for (int i = 0; i < messages.length; i++) {
  System.out.println("------------ Message " + (i + 1) + " ------------");
  // Here's the difference...
  Enumeration headers = messages[i].getAllHeaders();
  while (headers.hasMoreElements()) {
  Header h = (Header) headers.nextElement();
  System.out.println(h.getName() + ": " + h.getValue());
  }
  System.out.println();
  }
  }
} 

Output:

------------ Message ------------
Return-Path: <test@localhost>
Delivered-To: test@localhost
Received: from 192.168.10.111 ([192.168.10.111])
  by roseindi-iyo058 (JAMES SMTP Server 2.3.1with SMTP ID 18
  for <test@localhost>;
  Wed, 16 Jul 2008 14:33:32 +0000 (GMT)
Date: Wed, 16 Jul 2008 14:33:32 +0000 (GMT)
From: test@localhost
To: test@localhost
Message-ID: <6794265.0.1216195417823.JavaMail.komal@komal>
Subject: hi..!
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Download code

Related Tags for How to show all header information of message using Java Mail api:
javacapiclassdeleteobjectobjectsmethodscopyheadermailmethodfoldersusingfolderappendthismessageaiappshowexamplejavamailtoexamfetchstoreldwssheilcanpeinasmheadntsagecaletjadclesdeclareendallagepenpimehowobjppstoredtorxaxampsssadelfoldessatanyoldishallmpletchetcheaandarcopsavassthshoavstaphatctsfeecletcetcjepleplndodsonolo


More Tutorials from this section

Ask Questions?    Discuss: How to show all header information of message using Java Mail api   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.