How to show header of any message using Java Mail api

This Example shows you how to show header of any message using javamail api.

How to show header of any message using Java Mail api

How to show header of any message using Java Mail api

     

This Example shows you how to show 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.

In the code given below we have used some methods to retrieve header information of the message. Some of these are...

getFrom()returns the "From" attribute.
getReplyTo()returns addresses to which replies should be directed.
getSubject()returns the subject of this method.


HeaderClient.java

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

public class HeaderClient {

  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) + " ------------");
  // Get the headers
  String from = InternetAddress.toString(messages[i].getFrom());
  if (from != null) {
  System.out.println("From: " + from);
  }
  String replyTo = InternetAddress.toString(
  messages[i].getReplyTo());
  if (replyTo != null) {
  System.out.println("Reply-to: " + replyTo);
  }
  String to = InternetAddress.toString(
  messages[i].getRecipients(Message.RecipientType.TO));
  if (to != null) {
  System.out.println("To: " + to);
  }
  String cc = InternetAddress.toString(
  messages[i].getRecipients(Message.RecipientType.CC));
  if (cc != null) {
  System.out.println("Cc: " + cc);
  }
  String bcc = InternetAddress.toString(
  messages[i].getRecipients(Message.RecipientType.BCC));
  if (bcc != null) {
  System.out.println("Bcc: " + to);
  }
  String subject = messages[i].getSubject();
  if (subject != null) {
  System.out.println("Subject: " + subject);
  }
  Date sent = messages[i].getSentDate();
  if (sent != null) {
  System.out.println("Sent: " + sent);
  }
  Date received = messages[i].getReceivedDate();
  if (received != null) {
  System.out.println("Received: " + received);  
  }
  System.out.println();
  }
  }
} 

Output:

------------ Message 1 ------------
From: test@localhost
Reply-to: test@localhost
To: test@localhost
Subject: hi..!
Sent: Tue Jul 15 18:44:41 IST 2008

------------ Message 2 ------------
From: test
Reply-to: test
To: test@localhost
Subject: Resistance is futile.
Sent: Tue Jul 15 17:57:32 IST 2008

------------ Message 3 ------------
From: test@localhost
Reply-to: test@localhost
To: test@localhost
Subject: hi..!
Sent: Mon Jul 14 21:10:35 IST 2008

Download code