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.*; public class ReadMail {
public static void main(String args[]) throws Exception {
String host = "192.168.10.205"; // Get system properties // Get the default Session object. // Get a Store object that implements the specified protocol. //Connect to the current host using the specified username and
password. //Create a Folder object corresponding to the given name. // Open the Folder. Message[] message = folder.getMessages();
// Display message. System.out.println("------------ Message " + (i + 1) + " ------------"); System.out.println("SentDate : " + message[i].getSentDate()); InputStream stream = message[i].getInputStream(); folder.close(true); |
Output:
------------ Message 1
------------ ------------ Message 2
------------ ------------ Message 3
------------ |