Reading message using Java Mail
by , 04-25-2012 at 09:58 PM (2295 Views)
To read the messages by the help of javamail api. , consider the given example. Folder Objects holds the stored messages. Folder might consist of messages or folders or both. Methods are declared by the Folder class so that to fetch, copy, append, copy or delete the messages. In this program, few used methods are given below:
System.getProperties() system properties are obtained by this method.
Session.getDefaultInstance(properties) Default Session object are obtained by this method.
session.getStore("pop3") Store object is obtained by this method by which the pop3 protocol is implemented.
store.connect(host, user, password) Username/Password is used to make connection with current host.
store.getFolder("inbox") Inbox’s Folder object is created.
folder.open(Folder.READ_ONLY) opens Folder.
folder.getMessages() get all folder messages.
Java Code: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(); } }









Email Blog Entry
PDF to TIFF Conversion & Control...
05-24-2013, 11:39 AM in Java Software