Home Javamail Reading message using Java Mail

Related Tutorials


 
 

Share on Google+Share on Google+

Reading message using Java Mail

Advertisement
This Example shows you how to read message using javamail api.

Reading message using Java Mail

     

This Example shows you how to read message using javamail api. All messages are stored in Folder objects. A folder can contain folders or messages or both. The Folder class declares methods that fetch, append, copy and delete messages. These are some of the methods used in the program.

System.getProperties() this method get the  system properties.
Session.getDefaultInstance(properties) this method get the default Session object.
session.getStore("pop3") this method get a Store object that implements the pop3 protocol.
store.connect(host, user, password) Connect to the current host using the specified username and password.
store.getFolder("inbox") create a Folder object of the inbox.
folder.open(Folder.READ_ONLY) open the Folder.
folder.getMessages() get all messages for the folder.

ReadMail.java

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

public class ReadMail { 

  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[] message = folder.getMessages();

  // Display message.
 
for (int i = 0; i < message.length; i++) {

  System.out.println("------------ Message " + (i + 1) + " ------------");

  System.out.println("SentDate : " + message[i].getSentDate());
 
System.out.println("From : " + message[i].getFrom()[0]);
 
System.out.println("Subject : " + message[i].getSubject());
  System.out.print("Message : ");

  InputStream stream = message[i].getInputStream();
 
while (stream.available() != 0) {
 
System.out.print((char) stream.read());
 
}
 
System.out.println();
 
}

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


Output:

------------ Message 1 ------------
SentDate : Tue Jul 15 18:44:41 IST 2008
From : test@localhost
Subject : hi..!
Message : Hi ...... 

------------ Message 2 ------------
SentDate : Tue Jul 15 17:57:32 IST 2008From :
 test <test@localhost>
Subject : Resistance is futile.
Message : Resistance is futile. You will be assimilated!

------------ Message 3 ------------
SentDate : Mon Jul 14 21:10:35 IST 2008
From : test@localhost
Subject : hi..!
Message : Hi this is komal choudhary......

Download code

Advertisements

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: June 11, 2009

Related Tutorials

Discuss: Reading message using Java Mail   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:5
sammy
June 6, 2011
read email without opening it.

please help me.i want to know how to read email without opening it using java mail..
resh
March 5, 2012
partial output

I got the above output except the content of the message.. Can anybody please help me out
Sencer
April 5, 2012
Error

i am take it error is : connection time out..
Roshan Chaudhary
April 11, 2012
Java mail

why i am getting the error shown below. i have used "mail.informaticscollege.edu.np" as host and used pop3 as protocol. -------------------------------------------------------------- run: Exception in thread "main" javax.mail.MessagingException: Connect failed; nested exception is: java.io.IOException: Connect failed at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:210) at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) at ReadMail.main(ReadMail.java:24) Caused by: java.io.IOException: Connect failed at com.sun.mail.pop3.Protocol.<init>(Protocol.java:122) at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:261) at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:206) ... 3 more Java Result: 1 BUILD SUCCESSFUL (total time: 2 seconds)
Abdoul
September 16, 2013
Print email in order of date received or sent

Hi I tried your code it works very well and is very helpful to me. However, I noticed that it doesn't print in order of received emails. Can you please help me figure what to do to fix it. Also I wanted to ask you, I am now trying to move the read email to another folder do you know how I can achieve this? Thank you so much! Below is the output I get (See sent date...) ------------ Message 1 ------------ From: abdoul@localhost To: tom@localhost Sent: Sat Sep 14 23:28:44 EDT 2013 Subject: New email with attachments2 Message : I have some attachments for you again. ------------ Message 2 ------------ From: abdoul@localhost To: tom@localhost Sent: Sat Sep 14 23:38:01 EDT 2013 Subject: New email with attachments3 Message : I have some attachments for you again for the third time. ------------ Message 3 ------------ From: abdoul@localhost To: tom@localhost Sent: Sat Sep 14 23:06:24 EDT 2013 Subject: New email with attachments Message : I have some attachments for you. ------------ Message 4 ------------ From: abdoul@localhost To: tom@localhost Sent: Sun Sep 15 20:05:33 EDT 2013 Subject: New email with attachments - 09/15/13 Message : I have updated the attachments content. Check it out! ------------ Message 5 ------------ From: abdoul@localhost To: tom@localhost Sent: Sun Sep 15 17:12:27 EDT 2013 Subject: New email with attachments3 Message : I have some attachments for you again for the third time.
DMCA.com