Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Javamail Java Mail AttributeClient Example

Related Tutorials


 
 

Share on Google+Share on Google+

Java Mail AttributeClient Example

Advertisement
This Example shows you how to show attribute of any message using javamail api. All messages are stored in Folder objects.

Java Mail AttributeClient Example

     

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

Method used in the code given below:
getMessages() of Folder class is used to get all Message object.

After getting messages object we will use method to find attribute values of message.
getFrom() method of Message class returns "from" attribute.
getReplyTo()method returns addresses to which replies should be directed.
getRecipients(Message.RecipientType type) method returns all the recipient addresses of the given type.


AttributeClient.java

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

public class AttributeClient {

  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);

  // Get the messages from the server
  Message[] messages = folder.getMessages();

  // Display message.
  for (int i = 0; i < messages.length; i++) {
  System.out.println("------------ Message " + (i + 1) + " ------------");
  // Here's the big change...
  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 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);
  }

  System.out.println();
  // Here's the attributes...
  System.out.println("This message is approximately " + messages[i].getSize() + 
    " bytes long.");
  System.out.println("This message has approximately " + messages[i].getLineCount() + 
    " lines.");
  String disposition = messages[i].getDisposition();
  if (disposition == null); // do nothing
  else if (disposition.equals(Part.INLINE)) {
  System.out.println("This part should be displayed inline");
  } else if (disposition.equals(Part.ATTACHMENT)) {
  System.out.println("This part is an attachment");
  String fileName = messages[i].getFileName();
  if (fileName != null) {
  System.out.println("The file name of this attachment is " + fileName);
  }
  }
  String description = messages[i].getDescription();
  if (description != null) {
  System.out.println("The description of this message is " + description);
  }
  }

  folder.close(true);
  store.close();
  }
} 

Output:

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

This message is approximately 11 bytes long.
This message has approximately -lines.
------------ Message ------------
From: test@localhost
Reply-to: test@localhost
To: test@localhost
Subject: hi..!
Sent: Tue Jul 15 22:56:06 IST 2008

This message is approximately 11 bytes long.
This message has approximately -lines.
------------ Message ------------
From: test <test@localhost>
Reply-to: test <test@localhost>
To: test@localhost
Subject: Resistance is futile.
Sent: Tue Jul 15 17:57:32 IST 2008

This message is approximately 48 bytes long.
This message has approximately -lines.
------------ Message ------------
From: test
Reply-to: test
To: test@localhost
Subject: hi..!
Sent: Tue Jul 15 17:26:47 IST 2008

This message is approximately 11 bytes long.
This message has approximately -lines.
------------ Message ------------
From: test@localhost
Reply-to: test@localhost
To: test@localhost
Subject: hi..!
Sent: Mon Jul 14 21:54:28 IST 2008

This message is approximately 11 bytes long.
This message has approximately -lines.
------------ Message ------------
From: test <test@localhost>
Reply-to: test <test@localhost>
To: test@localhost
Subject: Resistance is futile.
Sent: Tue Jul 15 17:58:35 IST 2008

This message is approximately 48 bytes long.
This message has approximately -lines.
------------ Message ------------
From: test@localhost
Reply-to: test@localhost
To: test@localhost
Subject: hi..!
Sent: Tue Jul 15 17:07:54 IST 2008

This message is approximately 11 bytes long.
This message has approximately -lines.
------------ Message ------------
From: test@localhost
Reply-to: test@localhost
To: test@localhost
Subject: hi..!
Sent: Mon Jul 14 21:10:35 IST 2008

This message is approximately 34 bytes long.
This message has approximately -lines.

Download code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 19, 2008

Related Tutorials

Discuss: Java Mail AttributeClient Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com